3

Possible Duplicate:
How to iterate through two lists in parallel?

I have 2 lists:

l = ["a", "b", "c"]
m = ["x", "y", "z"]

And I want to iterate through both at the same time, something like this:

for e, f in l, m:
    print e, f

Must show:

a x
b y
c z

The thing is that is totally illegal. How can I do something like this? (In a Pythonic way)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83

1 Answers1

6

Look at itertools izip. It'll look like this

for i,j in izip( mylistA, mylistB ):
    print i + j

The zip function will also work but izip creates an iterator which does not force the creation of a third list.

wheaties
  • 35,646
  • 15
  • 94
  • 131