Which is faster and why? Is there a noticeable difference?
""" my_list=[1,2,3...N] """
for i in my_list:
do something
or
for i in range(1,N):
do something
It seems to me that in the second case the range function would be called N times which might be slower but I don't know how the in operator really works.
rangefunction does not get called N times. If you are using Python 3.x the second method is a bit faster and more memory efficient sincerangedoes not construct the whole list in advance. For Python 2.x you can usexrangefor the same effect.