← Python Intermediate
2 / 25
Lesson 2 — Dict & set comprehensions
Build maps fast
A dict comprehension works like a list comprehension, but builds a dictionary. Use curly braces instead of square ones and write key: value before the for. Each turn of the loop adds one key with its value.
nums = [1, 2, 3]
squares = {n: n * n for n in nums}
print(squares) # {1: 1, 2: 4, 3: 9}
Why it matters: turning a list into a lookup table (id to user, word to count) is one of the most common data tasks.
Want it another way?
Task
Build a dict that maps each word to its length and print it.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.