← back to unit
Medium
# basics loops
Looping Over a Dictionary
To loop through a dictionary's keys and values together, use .items():
ages = {"Sam": 10, "Zoe": 12}
for name, age in ages.items():
print(name, age)
prints:
Sam 10
Zoe 12
Your turn: a dictionary called scores containing {"Sam": 10, "Zoe": 12} is given. Loop through scores.items() and print each name and score, separated by a space (print(name, score)).
💡 need a hint?
pyb-loop-dict-items.py🔒 given lines are locked — write your code in between
loading...