13

Is the in operator's speed in python proportional to the length of the iterable?

So,

len(x) #10
if(a in x): #lets say this takes time A
    pass

len(y) #10000
if(a in y): #lets say this takes time B
    pass

Is A > B?

Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59

3 Answers3

38

A summary for in:

list - Average: O(n)
set/dict - Average: O(1), Worst: O(n)

See this for more details.

lennon310
  • 12,503
  • 11
  • 43
  • 61
  • so if i need to check if a string is present as a key in a dict. then it will take O(1). but if i need to see if a string is present in a list then O(n) right? – Anshu Dwibhashi Nov 27 '13 at 06:01
  • or even if i have a set of string and a list of string, x in set will be faster than x in list right? – Anshu Dwibhashi Nov 27 '13 at 06:02
  • when can the worst case occur? – Anshu Dwibhashi Nov 27 '13 at 06:05
  • 1
    I think when all the keys have the same hash value in the dict – lennon310 Nov 27 '13 at 06:09
  • in a set? how does that happen? – Anshu Dwibhashi Nov 27 '13 at 06:11
  • 1
    dicts and sets have very similar implementations in CPython - they're both based on hash codes. Worst-case behavior has *never* been seen outside of code deliberately designed to provoke it - but you need some rational faith in statistics ;-) If you *want* to provoke it, then, e.g., define your own class with a `__hash__` method that returns a constant. – Tim Peters Nov 27 '13 at 06:17
  • I had not thought about that worst case scenario when I opened this thread so seeing O(n) was a surprise. Interesting to know! – Tim Wilder Nov 27 '13 at 07:01
8

There's no general answer to this: it depends on the types of a and especially of b. If, for example, b is a list, then yes, in takes worst-case time O(len(b)). But if, for example, b is a dict or a set, then in takes expected-case time O(1) (i.e., constant time).

About "Is A > B?", you didn't define A or B. As above, there's no general answer to which of your in statements will run faster.

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
0

Assuming x and y are lists:

Is the in operator's speed in Python proportional to the length of the iterable?

Yes.

The time for in to run on a list of length n is O(n). It should be noted that it is O(1) for x in set and x in dict as they are hashed, and in is constant time.


Is A > B?

No.

Time A < Time B as 10 < 10000


Docs: https://wiki.python.org/moin/TimeComplexity

Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29