Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python double iteration

Tags:

python

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 with the ith line of the other file), I would want to do something like this:

file1 = csv.reader(open(filename1),...)
file2 = csv.reader(open(filename2),...)

for line1 in file1 and line2 in file2: #pseudo-code!
    if line1 != line2:
        print "files are not identical"
        break

What is the pythonic way of achieving this?


Edit: I am not using a file handler but rather a CSV reader (csv.reader(open(file),...)), and zip() doesn't seem to work with it...


Final edit: like @Alex M. suggested, zip() loads the files to memory on first iteration, so on big files this is an issue. On Python 2, using itertools solves the issue.

like image 347
Yuval Adam Avatar asked Mar 06 '10 17:03

Yuval Adam


People also ask

What are the iterations in Python?

An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__() .

Can you have two for loops in a function Python?

Nested For LoopsLoops can be nested in Python, as they can with other programming languages. The program first encounters the outer loop, executing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion.

Can Python list be iterated?

We can iterate through a list by using the range() function and passing the length of the list. It will return the index from 0 till the end of the list. The output would be the same as above.


2 Answers

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.

like image 161
Alex Martelli Avatar answered Sep 23 '22 12:09

Alex Martelli


I vote for using zip. The manual suggests "To loop over two or more sequences at the same time, the entries can be paired with the zip() function"

For example,

list_one = ['nachos', 'sandwich', 'name']
list_two = ['nachos', 'sandwich', 'the game']
for one, two in zip(list_one, list_two):
   if one != two:
      print "Difference found"
like image 23
JAL Avatar answered Sep 25 '22 12:09

JAL