Python zip - Iterate lists in parallel

You can iterate lists in parallel using zip in Python.

a = ['Apple', 'Microsoft', 'Google']
b = ['Mac', 'Windows', 'Android']

for t in zip(a, b):
    print(t)
    print(type(t))

# ('Apple', 'Mac')
# <class 'tuple'>
# ('Microsoft', 'Windows')
# <class 'tuple'>
# ('Google', 'Android')
# <class 'tuple'>

The zip() returns a zip object, an iterator of tuples. Each tuple is a pair of items in zipped lists with the same index.

Two lists can have the different number of elements.

a = ['Apple', 'Microsoft', 'Google']
b = ['Mac', 'Windows']

for t in zip(a, b):
    print(t)

# ('Apple', 'Mac')
# ('Microsoft', 'Windows')

More than two lists can be zipped.

a = ['Apple', 'Microsoft', 'Google']
b = ['Mac', 'Windows', 'Android']
c = ['Safari', 'Edge', 'Chrome']

for t in zip(a, b, c):
    print(t)

# ('Apple', 'Mac', 'Safari')
# ('Microsoft', 'Windows', 'Edge')
# ('Google', 'Android', 'Chrome')

Python List

Python Tutorial