0Pricing
Python Academy · Lesson

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

The same comprehension syntax works for sets and dicts, producing unique elements and key-value mappings respectively.

Set Comprehension Syntax

{expression for item in iterable} uses curly braces without a colon. Result is a set — unordered, unique.
unique_lens = {len(w) for w in ['apple','banana','fig']}
print(unique_lens)

Deduplication with Set Comprehension

Any duplicate expressions are silently dropped. {x%3 for x in range(9)} gives {0,1,2}.
mods = {x % 3 for x in range(9)}
print(mods)

Set Comprehension with Condition

{x for x in range(20) if x%2==0 and x%3==0} gives numbers divisible by both 2 and 3.
print({x for x in range(20) if x%2==0 and x%3==0})

Dict Comprehension Syntax

{key_expr: val_expr for item in iterable} uses key:value before for. Creates a dict.
squares = {x: x**2 for x in range(5)}
print(squares)

Dict Comprehension with Condition

{k: v for k, v in d.items() if v > 0} filters a dict to only positive values.
d = {'a': 1, 'b': -2, 'c': 3}
pos = {k: v for k, v in d.items() if v > 0}
print(pos)

Inverting a Dict

{v: k for k, v in d.items()} swaps keys and values. Requires all values to be unique and hashable.
d = {'a': 1, 'b': 2, 'c': 3}
print({v: k for k, v in d.items()})

Grouping with Dict Comprehension

{letter: [w for w in words if w[0]==letter] for letter in 'abc'} groups words by first letter.
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

{k: v for k, v in zip(keys, vals)} is cleaner than dict(zip(keys, vals)) when you need transformation.
keys = ['x', 'y', 'z']
vals = [1, 4, 9]
print({k: v**0.5 for k, v in zip(keys, vals)})

Nested Dict Comprehension

{i: {j: i*j for j in range(3)} for i in range(3)} creates a multiplication table as nested dicts.
table = {i: {j: i*j for j in range(3)} for i in range(3)}
print(table)

When to Use Each

Set comprehension when you need unique values. Dict when you need key-value mapping. List when order matters.
words = ['hello', 'world', 'hello']
unique = {w for w in words}
lengths = {w: len(w) for w in unique}
print(lengths)

Quick Check

What is the key syntactic difference between a set and dict comprehension?

Recap

Set comprehension: {expr for x in it}. Dict comprehension: {k: v for x in it}. Both support if filtering. Dict requires unique, hashable keys.

Keep Going

Excellent! Continue to the next lesson to deepen your skills.

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

  1. List Comprehensions
  2. Set and Dict Comprehensions
  3. Generator Expressions
  4. The yield Keyword
← Back to Python Academy