sinθ
sinθ

Reputation: 11503

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.

Upvotes: 9

Views: 5057

Answers (4)

ThatGuyRussell
ThatGuyRussell

Reputation: 1381

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.

Upvotes: 4

Anand S Kumar
Anand S Kumar

Reputation: 91009

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 .

Upvotes: 0

John Smith
John Smith

Reputation: 7407

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")

Upvotes: 0

user2357112
user2357112

Reputation: 282043

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.

Upvotes: 13

Related Questions