Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print content of two lists

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading