Python Loop Through a Dictionary Quiz

This Python quiz is about looping over dictionaries: keys, values(), items(), comprehensions and sorting by value. It also checks the mistakes that bite in real code, like changing a dict while you loop over it.

Medium 10 questions ~6 minutes Pass at 80% Python 3.12

Choose a mode

Playing as a guest: sign in to keep your XP and streak.

Sample question

Question 1 of 10

What does this print?

d = {"a": 1, "b": 2}
out = []
for x in d:
    out.append(x)
print(out)
Show the answer

B. ['a', 'b'] Looping over a dict directly gives its keys, in insertion order. Use d.values() for the values or d.items() for key-value pairs.

What this quiz covers

  • What a plain for loop over a dict gives you
  • Unpacking keys and values with items()
  • Dict comprehensions and inverting a dict
  • Sorting keys by their values
  • Changing a dict during a loop

Questions people ask

Do I need an account?

No. Anyone can play. Sign in to keep your XP, streak, badges and history.

Which Python version?

Python 3.12. Every question about output was checked by running the code.

Can I retake it?

Yes. Your best score is kept, and the questions and answers are shuffled each time.