There's a huge difference.
- Sets can't contain duplicates
- Sets are unordered
- In order to find an element in a set, a hash lookup is used (which is why sets are unordered). This makes
__contains__(inoperator) a lot more efficient for sets than lists. - Sets can only contain hashable items (see #3). If you try:
set(([1],[2]))you'll get aTypeError.
In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order.
Also note that if you don't care about order, etc, you can use
new_set = myset.intersection(mylist)
to get the intersection between a set and a list.
There's a huge difference.
- Sets can't contain duplicates
- Sets are unordered
- In order to find an element in a set, a hash lookup is used (which is why sets are unordered). This makes
__contains__(inoperator) a lot more efficient for sets than lists. - Sets can only contain hashable items (see #3). If you try:
set(([1],[2]))you'll get aTypeError.
In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order.
Also note that if you don't care about order, etc, you can use
new_set = myset.intersection(mylist)
to get the intersection between a set and a list.
sets — Unordered collections of unique elements
lists - ordered collections of elements
sets allows you to do operations such as intersection, union, difference, and symmetric difference, i.e operations of math's set theory. Sets doesn't allow indexing and are implemented on hash tables.
lists are really variable-length arrays, not Lisp-style linked lists. In lists the elements are accessed by indices.
Python Sets vs Lists - Stack Overflow
What makes lists different than sets?
What is difference between list and set in python? - Stack Overflow
Differences in terms of performance between list, set, and numpy arrays?
It depends on what you are intending to do with it.
Sets are significantly faster when it comes to determining if an object is present in the set (as in x in s), but its elements are not ordered so you cannot access items by index as you would in a list. Sets are also somewhat slower to iterate over in practice.
You can use the timeit module to see which is faster for your situation.
Lists are slightly faster than sets when you just want to iterate over the values.
Sets, however, are significantly faster than lists if you want to check if an item is contained within it. They can only contain unique items though.
It turns out tuples perform in almost exactly the same way as lists, except for their immutability.
Iterating
>>> def iter_test(iterable):
... for i in iterable:
... pass
...
>>> from timeit import timeit
>>> timeit(
... "iter_test(iterable)",
... setup="from __main__ import iter_test; iterable = set(range(10000))",
... number=100000)
12.666952133178711
>>> timeit(
... "iter_test(iterable)",
... setup="from __main__ import iter_test; iterable = list(range(10000))",
... number=100000)
9.917098999023438
>>> timeit(
... "iter_test(iterable)",
... setup="from __main__ import iter_test; iterable = tuple(range(10000))",
... number=100000)
9.865639209747314
Determine if an object is present
>>> def in_test(iterable):
... for i in range(1000):
... if i in iterable:
... pass
...
>>> from timeit import timeit
>>> timeit(
... "in_test(iterable)",
... setup="from __main__ import in_test; iterable = set(range(1000))",
... number=10000)
0.5591847896575928
>>> timeit(
... "in_test(iterable)",
... setup="from __main__ import in_test; iterable = list(range(1000))",
... number=10000)
50.18339991569519
>>> timeit(
... "in_test(iterable)",
... setup="from __main__ import in_test; iterable = tuple(range(1000))",
... number=10000)
51.597304821014404
I use sets sometimes to remove duplicate entries from a list.
But a set, is not a list.
The difference that I see is I cannot call an item from a set by index. But a set can be iterated and I get similar results to a list, but so can any string.
So I guess the question is. What makes a set a set? Is there a better method for remove duplicates from a list?