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?
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?
A summary for in:
See this for more details.
There's no general answer to this: it depends on the types of
a
and especially ofb
. If, for example,b
is a list, then yes,in
takes worst-case timeO(len(b))
. But if, for example,b
is a dict or a set, thenin
takes expected-case timeO(1)
(i.e., constant time).About "Is A > B?", you didn't define
A
orB
. As above, there's no general answer to which of yourin
statements will run faster.