Let’s say I have two or more lists of same length. What’s a good way to iterate through them?
a, b are the lists.
for i, ele in enumerate(a):
print ele, b[i]
or
for i in range(len(a)):
print a[i], b[i]
or is there any variant I am missing?
Is there any particular advantages of using one over other?
The usual way is to use
zip():This will stop when the shorter of the two iterables
aandbis exhausted. Also worth noting:itertools.izip()(Python 2 only) anditertools.izip_longest()(itertools.zip_longest()in Python 3).