-1

CODE:

students = ['John', 'Tom', 'Toby', 'Sandy', 'Shawn']
marks = [77, 79, 80, 71, 17]
for student in students:
    for mark in marks:
        print(f'{student}: {mark}')

RESULT:

John: 77
John: 79
John: 80
John: 71
John: 17
Tom: 77
Tom: 79
Tom: 80
Tom: 71
Tom: 17
Toby: 77
Toby: 79
Toby: 80
Toby: 71
Toby: 17
Sandy: 77
Sandy: 79
Sandy: 80
Sandy: 71
Sandy: 17
Shawn: 77
Shawn: 79
Shawn: 80
Shawn: 71
Shawn: 17

I want to generate the result as:

John: 77
Tom: 79
Toby: 80
Sandy: 71
Shawn: 17
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    use `zip` to pair them up `for student, mark in zip(students, marks): print(f'{student}:{mark}')` – Alexander Jan 01 '23 at 04:12
  • Currently, the way your code is structured, you are looping over each student, and then all of the marks. You finish looping over the marks for each student before moving on to the next one! What you should do, since both arrays are the same length, is make one loop, and pull values from both arrays in that one loop. – Cassidy Jan 01 '23 at 04:15

1 Answers1

0
dict(zip(students,marks))
{'John': 77, 'Tom': 79, 'Toby': 80, 'Sandy': 71, 'Shawn': 17}
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44