Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set vs. set python

What's the difference between set("a") and sets.Set("a")? Their types are different, but they seem to do the same thing.

I can't find any resources online about it, but I've seen both used in examples.

like image 776
sinθ Avatar asked Aug 20 '15 01:08

sinθ


People also ask

Can we compare two sets in Python?

Python Set | difference()The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The function difference() returns a set that is the difference between two sets.

What is set () in Python?

set() method is used to convert any of the iterable to sequence of iterable elements with distinct elements, commonly called Set. Parameters : Any iterable sequence like list, tuple or dictionary. Returns : An empty set if no element is passed.

Can you have a set in a set in Python?

To represent a set of sets, the inner sets must be frozenset objects for the reason that the elements of a set must be hashable (all of Python's immutable built-in objects are hashable). frozenset is immutable and set is mutable. Save this answer.

What is the difference between set method () and {} in Python?

The {} syntax treats each variable as a single element, rather than iterating over it. The set() function is frequently used to convert from one kind of collection to a set of its elements, that's why it iterates over it.


2 Answers

You've tagged this Python 3, so the difference is that sets doesn't exist. Use set.

In Python 2, the difference is that sets is deprecated. It's the old, slow, not-as-good version. Use set. This is explained in the documentation for the sets module, which comes up instantly on a search for Python sets.

like image 58
user2357112 supports Monica Avatar answered Sep 23 '22 07:09

user2357112 supports Monica


The built in set() was based on the old sets.Set() and runs faster.
Both 'do' the same thing, though in Python 3 the 'sets' module no longer exists.

Here is the answer directly from The Python 2 Library:
The built-in set and frozenset types were designed based on lessons learned from the sets module. The key differences are:

Set and ImmutableSet were renamed to set and frozenset.

- There is no equivalent to BaseSet. Instead, use isinstance(x, (set, frozenset)).

- The hash algorithm for the built-ins performs significantly better (fewer collisions) for most datasets.

- The built-in versions have more space efficient pickles.

- The built-in versions do not have a union_update() method. Instead, use the update() method which is equivalent.

- The built-in versions do not have a _repr(sorted=True) method. Instead, use the built-in repr() and sorted() functions: repr(sorted(s)).

- The built-in version does not have a protocol for automatic conversion to immutable. Many found this feature to be confusing and no one in the community reported having found real uses for it.

like image 37
ThatGuyRussell Avatar answered Sep 22 '22 07:09

ThatGuyRussell