How efficient/fast is Python's 'in'? (Time Complexity wise)

John picture John · Oct 16, 2012 · Viewed 73.5k times · Source

In Python, what is the efficiency of the in keyword, such as in:

a = [1, 2, 3]
if 4 in a:
  ...

Answer

Eduardo picture Eduardo · Oct 16, 2012

The complexity for lists is:

O(n)

For sets it is:

O(1)

http://wiki.python.org/moin/TimeComplexity