Another cool shortcut method in Python! So, you have two lists. You want to pair them up, in order. That is, you want to associate the first item in list A with the first item in list B, the second item in list A with the second item in list B, and so on.
grades = ["B", "D", "A", "B"]
students = ["Ken", "Ben", "Wendy", "Sandy"]
for student, grade in zip(students, grades):
print( student + ": " + grade )
This would be nice for creating a list of dictionaries, which is essentially JSON format:
student_records = []
for student, grade in zip(students, grades):
student_records.append( {"student":student, "grade":grade} )
for record in student_records:
print(record)
{'student': 'Ken', 'grade': 'B'}
{'student': 'Ben', 'grade': 'D'}
{'student': 'Wendy', 'grade': 'A'}
{'student': 'Sandy', 'grade': 'B'}
So easy! zip()
does it all.