Python Sets vs Lists Python Sets vs Lists python python

Python Sets vs Lists


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 are slower than lists when it comes to iterating over their contents.

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


Set wins due to near instant 'contains' checks: https://en.wikipedia.org/wiki/Hash_table

List implementation: usually an array, low level close to the metal good for iteration and random access by element index.

Set implementation: https://en.wikipedia.org/wiki/Hash_table, it does not iterate on a list, but finds the element by computing a hash from the key, so it depends on the nature of the key elements and the hash function. Similar to what is used for dict. I suspect list could be faster if you have very few elements (< 5), the larger element count the better the set will perform for a contains check. It is also fast for element addition and removal. Also always keep in mind that building a set has a cost !

NOTE: If the list is already sorted, searching the list could be quite fast on small lists, but with more data a set is faster for contains checks.