What is the pythonic way of iterating simultaneously over two lists?
Suppose I want to compare two files line by line (compare each ith line in one file
In Python 2, you should import itertools and use its izip:
with open(file1) as f1:
with open(file2) as f2:
for line1, line2 in itertools.izip(f1, f2):
if line1 != line2:
print 'files are different'
break
with the built-in zip, both files will be entirely read into memory at once at the start of the loop, which may not be what you want. In Python 3, the built-in zip works like itertools.izip does in Python 2 -- incrementally.