Recall & Review
beginner
What is dictionary comprehension in Python?
Dictionary comprehension is a concise way to create dictionaries by writing a single expression that generates key-value pairs.
Click to reveal answer
beginner
Why do programmers use dictionary comprehension instead of a for-loop?
Because dictionary comprehension is shorter, easier to read, and often faster than using a for-loop to build dictionaries.
Click to reveal answer
beginner
How does dictionary comprehension improve code readability?
It puts the logic of creating key-value pairs in one clear line, making the code easier to understand at a glance.
Click to reveal answer
intermediate
Can dictionary comprehension include conditions? Why is this useful?
Yes, it can include conditions to filter which items are added. This helps create dictionaries with only the needed data.
Click to reveal answer
beginner
Give a real-life example where dictionary comprehension is helpful.
For example, creating a dictionary of student names and their scores from a list, quickly and clearly, without writing many lines of code.
Click to reveal answer
What is the main benefit of using dictionary comprehension?
✗ Incorrect
Dictionary comprehension helps write shorter and clearer code to create dictionaries.
Which of these is a correct use of dictionary comprehension?
✗ Incorrect
Option D shows dictionary comprehension syntax with keys and values.
Can dictionary comprehension include an if condition to filter items?
✗ Incorrect
Dictionary comprehension can include if conditions to select which items to add.
Which is NOT a reason to use dictionary comprehension?
✗ Incorrect
Dictionary comprehension aims to make code easier, not harder, to read.
What does this dictionary comprehension do? {x: x**2 for x in range(4)}
✗ Incorrect
It creates a dictionary where each key is a number from 0 to 3 and the value is its square.
Explain why dictionary comprehension is preferred over traditional loops for creating dictionaries.
Think about how it helps make code simpler and clearer.
You got /4 concepts.
Describe a situation where using dictionary comprehension would make your code easier to understand.
Imagine you have a list of items and want to quickly make a dictionary from it.
You got /4 concepts.