Andrew Anjun Hou
Andrew Anjun Hou

Reputation: 55

Printing simultaneously from 2 lists

Assume the following 2 lists:

l1 = ['a', 'b', 'c', 'd', 'e']
l2 = ['f', 'g', 'h', 'i']

I am looking for the following output

a f
b g
c h
d i
e

Here is what I have tried

for x, y in l1, l2:
    print(x, y)

but this has too many items to unpack, does anybody know how I could get the output I need?

Upvotes: 0

Views: 36

Answers (1)

zvi
zvi

Reputation: 4706

Use python zip_longest:

from itertools import zip_longest

l1 = ['a', 'b', 'c', 'd', 'e']
l2 = ['f', 'g', 'h', 'i']

print (zip_longest (l1, l2, fillvalue = ''))

Upvotes: 5

Related Questions