Python increment two lines at once

I want to be able to simultaneously interact with both list1 = list('asdf'), and with list2 = list('qwer'). What is the best approach?

for i, p in list1, list2:
    print(i,p)

Where it iwill be an increment list1, but pwill increase list2.

+5
source share
1 answer

Use zip(or itertools.izipif the two lists are large):

for i, p in zip(list1, list2):
    print(i, p)

Alternatively, if it list1may not be the same length as list2, use izip_longestfromitertools

+13
source

All Articles