I’m not sure how to title this post, so I tried to make it as accurate as possible. I’m trying to print the content of two lists of the same length.
Let’s say we have two lists:
name = ['Tyler', 'Daniel', 'Connor', 'Jeff']
number = ['64', '34', '76', '24']
Let’s now say I’d like to print out the content of these lists, but each line should match with the position of the content. That meaning, I’d like it to print out like this:
Tyler 64
Daniel 34
Connor 76
Jeff 24
I’ve tried doing this but I have not figured out a way around it. I tried doing for loops, but I couldn’t find a way to combine two for loops, one for each list. I tried:
print(name[i]+" "+number[j] for i in name and for j in number)
But I couldn’t get it to work. Do you have any ideas?
>Solution :
zip() will do the "matching up" for you by evenly iterating over all its arguments:
names = ['Tyler', 'Daniel', 'Connor', 'Jeff']
numbers = ['64', '34', '76', '24']
for name, number in zip(names, numbers):
print(f"{name} {number}")