9

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.

asked Aug 20, 2015 at 1:40

4 Answers 4

13

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.

answered Aug 20, 2015 at 1:45
Sign up to request clarification or add additional context in comments.

Comments

4

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.

answered Aug 20, 2015 at 1:45

Comments

0

Set is built in now, and can be used without having to import the 'sets' module explicitly.

Reference:

Python - can't import Set from sets ("no module named sets")

answered Aug 20, 2015 at 1:45

Comments

0

There is not much difference, and you should use the builtin set or frozenset , instead of sets module.

The sets module documentation itself says -

Deprecated since version 2.6: The built-in set/frozenset types replace this module.

And there is no sets module in Python 3.x , only Python 2 .

answered Aug 20, 2015 at 1:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.