SyncMaster
SyncMaster

Reputation: 9966

For loop with multiple conditions in Python

I have 3 lists of equal sizes (List1,2 and 3). I want to iterate through the list and and perform operations on each of the items. Like

for x in List1, y in List2, z in List3:
    if(x == "X" or x =="x"):
         //Do operations on y
    elif(y=="Y" or y=="y"):
         //Do operations on x,z

So I want to traverse the list only for "Length of List1 or 2 or size" and then perform operations on x,y and z. How can I do this using Python?

Edit: Python Version 2.6.6

Upvotes: 4

Views: 10097

Answers (2)

Cat Plus Plus
Cat Plus Plus

Reputation: 129984

import itertools
for x, y, z in itertools.izip(List1, List2, List3):
    # ...

Or just zip in Python 3.

Upvotes: 8

newtover
newtover

Reputation: 32094

>>> map(lambda x, y, z: (x, y, z), range(0, 3), range(3, 6), range(6, 9))
[(0, 3, 6), (1, 4, 7), (2, 5, 8)]

Upvotes: 0

Related Questions