Set and Dict Comprehensions
Apply comprehension patterns to sets and dictionaries.
Set and Dict Comprehensions is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction
Set Comprehension Syntax
unique_lens = {len(w) for w in ['apple','banana','fig']}
print(unique_lens)Deduplication with Set Comprehension
mods = {x % 3 for x in range(9)}
print(mods)Set Comprehension with Condition
print({x for x in range(20) if x%2==0 and x%3==0})Dict Comprehension Syntax
squares = {x: x**2 for x in range(5)}
print(squares)Dict Comprehension with Condition
d = {'a': 1, 'b': -2, 'c': 3}
pos = {k: v for k, v in d.items() if v > 0}
print(pos)Inverting a Dict
d = {'a': 1, 'b': 2, 'c': 3}
print({v: k for k, v in d.items()})Grouping with Dict Comprehension
words = ['apple','ant','banana','cherry','cat']
grouped = {l: [w for w in words if w.startswith(l)] for l in 'abc'}
print(grouped)Combining Two Lists into a Dict
keys = ['x', 'y', 'z']
vals = [1, 4, 9]
print({k: v**0.5 for k, v in zip(keys, vals)})Nested Dict Comprehension
table = {i: {j: i*j for j in range(3)} for i in range(3)}
print(table)When to Use Each
words = ['hello', 'world', 'hello']
unique = {w for w in words}
lengths = {w: len(w) for w in unique}
print(lengths)Quick Check
Recap
Keep Going
Frequently asked questions
Is the “Set and Dict Comprehensions” lesson free?
Yes — the full text of “Set and Dict Comprehensions” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Set and Dict Comprehensions”?
Apply comprehension patterns to sets and dictionaries. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Set and Dict Comprehensions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- List Comprehensions
- Set and Dict Comprehensions
- Generator Expressions
- The yield Keyword