Home > Enterprise >  How to display two lists at the same time
How to display two lists at the same time

Time:10-28

I have two lists

x = ['34', '22', '45']
y = ['red', 'blue', 'grean']

I need to output these two lists together

34, red
22, blue
45, grean

i tried to get it all through for in

for a, b in x, y:
    print(a, b)

but i get an error

too many values to unpack (expected 2)

CodePudding user response:

x = ['34', '22', '45']
y = ['red', 'blue', 'grean']

for a, b in zip(x, y):
    print(f"{a}, {b}")

Output:

34, red
22, blue
45, grean

CodePudding user response:

Try this:

x = ['34', '22', '45']
y = ['red', 'blue', 'green'] // you made a typo in 'green' so i fixed it
for i, x in enumerate(x):
    print(f"{x}, {y[i]}")

It returns this:

34, red
22, blue
45, green
  • Related