- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AI / Machine LearningNeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Learn RustOwnership, traits, error handling, and threads Learn GoSlices, interfaces, goroutines, and channels Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- QuizzesNew
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
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.
Sample question
Question 1 of 10What 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
forloop 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.