Just an academic question. I’m curious what version of this code is better (faster) in Python?
var = random.randint(1, 10)
# First version
if var in [2, 5, 9]:
print "First: 2, 5 or 9"
# Second version
if var == 2 or number == 5 or number == 9:
print "Second: 2, 5 or 9"
This is very simple example, but what if variable var is not a number, but string?
var = 'aaa'
# First version
if var in ['aaa', 'zzz', 'eee']:
print "String"
What about some more complicated objects (not only numbers or string, but some class with very time-consuming comparison)?
And what happens inside Python compiler? I suppose if var in list is executed like:
for l in list:
if l == var:
print "String"
So in my opinion both versions in first example are the same (speed). Am I right?
Timing the code will show which is fastest. Use the timeit module for that:
If you want to improve your instincts on what Python is doing under-the-hood and knowing which code is fastest, start by disassembling code:
This will show that the this-or-this-or-that code does more steps than the list.__contains__ version.
FWIW, you may want too consider using sets instead of a list. Their O(1) lookups tend to beat lists depending on key frequency (is the first element of the list the most likely match), and on how expensive the hash function is, and on the number of elements (sets scale up better than lists with their O(n) search:
On my machine, sets did not help for searching a small number of integer elements: