Related to this post: If in List vs For loop (performance)
While timing the execution, test1() is at least twice as slow as test2(). There are fewer lines in test2(). Is it due to this? Besides it and also 'in' being a pythonic way of doing things. Is 'in' mostly more efficient than similar loop structure, and should it be preferred over loops for list and dict checks for performance reasons?
import timeit
a = [n for n in range(1, 1000)]
s = a[len(a) - 1]
def test1():
for i in a:
if i == s:
break
def test2():
if s in a:
pass
if __name__ == '__main__':
n = 10000
print("test1 milli-sec per loop: ", timeit.timeit("test1()",
setup="from __main__ import test1", number=n) / n * 1000)
print("test2 milli-sec per loop: ", timeit.timeit("test2()",
setup="from __main__ import test2", number=n) / n * 1000)