You can se zip to iterate over multiple lists simultaneously /u/FixPractical1121 Python Education

names = ['Alice', 'Bob', 'Charlie'] scores = [85, 90, 78] for name, score in zip(names, scores): print(f"{name} scored {score}") 

And if you wonder what happens when there are 2 different length lists:

from itertools import zip_longest names = ['Alice', 'Bob'] scores = [85, 90, 78] for name, score in zip_longest(names, scores, fillvalue='N/A'): print(f"{name} scored {score}") 

Output:

Alice scored 85 Bob scored 90 N/A scored 78 

submitted by /u/FixPractical1121
[link] [comments]

​r/learnpython names = [‘Alice’, ‘Bob’, ‘Charlie’] scores = [85, 90, 78] for name, score in zip(names, scores): print(f”{name} scored {score}”) And if you wonder what happens when there are 2 different length lists: from itertools import zip_longest names = [‘Alice’, ‘Bob’] scores = [85, 90, 78] for name, score in zip_longest(names, scores, fillvalue=’N/A’): print(f”{name} scored {score}”) Output: Alice scored 85 Bob scored 90 N/A scored 78 submitted by /u/FixPractical1121 [link] [comments] 

names = ['Alice', 'Bob', 'Charlie'] scores = [85, 90, 78] for name, score in zip(names, scores): print(f"{name} scored {score}") 

And if you wonder what happens when there are 2 different length lists:

from itertools import zip_longest names = ['Alice', 'Bob'] scores = [85, 90, 78] for name, score in zip_longest(names, scores, fillvalue='N/A'): print(f"{name} scored {score}") 

Output:

Alice scored 85 Bob scored 90 N/A scored 78 

submitted by /u/FixPractical1121
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *