What if you could turn a list into a dictionary with just one neat line of code?
Why Basic dictionary comprehension in Python? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a list of items and you want to create a dictionary where each item is a key and its length is the value. Doing this by hand means writing a loop and adding each pair one by one.
Writing loops for this task is slow and easy to mess up. You might forget to add a key or value, or write extra lines of code that make your program harder to read and maintain.
Basic dictionary comprehension lets you create the whole dictionary in one clear, short line. It makes your code cleaner and faster to write, while doing the same job perfectly.
result = {}
for item in items:
result[item] = len(item)result = {item: len(item) for item in items}It enables you to transform lists into dictionaries quickly and clearly, making your code easier to understand and maintain.
Suppose you have a list of fruit names and want to know the length of each name. Dictionary comprehension lets you get this info instantly without writing extra loops.
Manual loops for building dictionaries are slow and error-prone.
Dictionary comprehension creates dictionaries in one simple line.
This makes your code cleaner, shorter, and easier to read.
Practice
{x: x*x for x in range(3)}Solution
Step 1: Understand the loop and key-value pairs
The comprehension loops over numbers 0, 1, 2 and uses each number as the key.Step 2: Calculate the value for each key
Each value is the square of the key (x*x), so values are 0, 1, 4 respectively.Final Answer:
Creates a dictionary with numbers 0 to 2 as keys and their squares as values -> Option CQuick Check:
Dictionary comprehension = key: x, value: x*x [OK]
- Confusing dictionary with list or set comprehension
- Mixing keys and values in comprehension
- Thinking it creates a list instead of a dictionary
Solution
Step 1: Identify correct dictionary comprehension syntax
Dictionary comprehensions use curly braces with key:value pairs and a for loop inside.Step 2: Check each option
{x: 2*x for x in range(1, 4)} uses curly braces and colon correctly. Options B, C, D use wrong brackets or arrow syntax.Final Answer:
{x: 2*x for x in range(1, 4)} -> Option BQuick Check:
Curly braces + key:value + for loop = correct syntax [OK]
- Using square brackets instead of curly braces
- Using arrow (=>) instead of colon for key-value
- Using parentheses which create generator expressions
nums = [1, 2, 3]
squares = {n: n**2 for n in nums if n % 2 != 0}
print(squares)Solution
Step 1: Understand the filtering condition
The comprehension includes only numbers where n % 2 != 0, meaning odd numbers only (1 and 3).Step 2: Calculate squares for filtered numbers
Squares are 1*1=1 and 3*3=9, so dictionary is {1:1, 3:9}.Final Answer:
{1: 1, 3: 9} -> Option AQuick Check:
Filter odd numbers and square them = {1:1, 3:9} [OK]
- Including even numbers by mistake
- Confusing list comprehension output with dictionary
- Ignoring the if condition in comprehension
result = {x, x*2 for x in range(3)}Solution
Step 1: Check syntax for key-value pairs
Dictionary comprehension requires a colon ':' between key and value, but here a comma ',' is used.Step 2: Confirm other parts are correct
Curly braces and range(3) are correct, so the error is the missing colon.Final Answer:
Missing colon between key and value -> Option AQuick Check:
Key:value pairs need colon, not comma [OK]
- Using comma instead of colon between key and value
- Confusing set comprehension with dictionary comprehension
- Using wrong brackets for comprehension
Which code correctly does this?
words = ['cat', 'house', 'dog', 'elephant']
Solution
Step 1: Understand filtering condition syntax
The if condition must come after the for loop to filter words by length correctly.Step 2: Check each option's syntax
{word: len(word) for word in words if len(word) > 3} correctly places 'if len(word) > 3' after the for loop. Others have syntax errors or wrong condition placement.Final Answer:
{word: len(word) for word in words if len(word) > 3} -> Option DQuick Check:
Filter after for loop with if condition [OK]
- Placing if condition before for loop
- Using invalid syntax like 'where' instead of 'if'
- Comparing word string directly to number
