Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 235
SyncMaster Avatar asked Feb 23 '23 09:02

SyncMaster


1 Answers

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

Or just zip in Python 3.

like image 82
Cat Plus Plus Avatar answered Feb 25 '23 00:02

Cat Plus Plus