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.
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.
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.
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With