Dict and Set Comprehensions

The same comprehension syntax that builds lists, building dictionaries and sets instead.

Overview

The same comprehension syntax that builds lists, building dictionaries and sets instead.

In this guide
  1. Three forms, one idea
  2. Dict comprehensions
  3. Duplicate keys do not complain
  4. The empty-braces trap, again
  5. When to use them
  6. Inverting and filtering a mapping
  7. Set comprehensions, and what they are for
  8. When a loop is still better
  9. A worked example: a lookup table and a grouping
  10. Scope, and the variable that does not leak
  11. Sets, and the operations that follow
  12. Nesting, and the two conditionals
  13. What is built, and when it matters
  14. Readability, stated as a limit
  15. Questions people ask about Dict and Set Comprehensions
  16. Dict and Set Comprehensions: recap in one screen

dict_comprehension.py

dict_comprehension.py Python 3
Output

                    

set_comprehension.py

set_comprehension.py Python 3
Output

                    

Worth knowing

A colon in the expression makes it a dict comprehension; no colon makes a set.
{} is still an empty dict. There is no empty-set literal.
Duplicate keys are not an error - the last one silently wins.
{v: k for k, v in d.items()} inverts a dictionary in one line.

Dict and Set Comprehensions: A Practical Guide

The comprehension syntax isn't a list feature. The same shape builds dicts and sets. Only the brackets and the formula change.

Three forms, one idea

[n * n for n in nums]    # list
{n * n for n in nums}    # set
{n: n * n for n in nums} # dict

Square brackets build a list. Braces build a set. Braces with a key: value expression in the slot build a dict. The for clause and any trailing if work the same way in all three.

If you already know list comprehensions, dict and set comprehensions are the same idea with different brackets. The shape is identical: an expression, a for clause, and an optional if filter. The only difference is what container the result lands in.

A set comprehension drops duplicates automatically. If the expression produces the same value twice, the set keeps one copy. That makes set comprehensions the shortest way to extract unique values from a sequence. {word.lower() for word in words} gives you every distinct lowercase word in one line.

A dict comprehension needs a colon in the expression slot to separate keys from values. {name: len(name) for name in names} maps each name to its length. {k: v for k, v in pairs if v > 0} filters a list of key-value tuples into a dictionary. If two entries produce the same key, the last one wins silently.

One common use for dict comprehensions is inverting a dictionary: swapping keys and values. {v: k for k, v in original.items()} turns {"a": 1, "b": 2} into {1: "a", 2: "b"}. Another common use is building a lookup table from a list, where the key is some property of each item.

The braces {} alone create an empty dictionary, not an empty set. That is a historical accident. An empty set requires set(). A set comprehension with at least one element uses braces without ambiguity because the expression has no colon.

This page covers the syntax of dict and set comprehensions, filtering with if, inverting dictionaries, building lookup tables, and the empty-braces gotcha.

Dict comprehensions

{w: len(w) for w in words}

The colon is the whole difference. All before it's the key, all after is the value. Both are plain expressions tested per item.

Two patterns come up constantly. Inverting:

{v: k for k, v in prices.items()}

and building from parallel lists:

{k: v for k, v in zip(keys, values)}

though dict(zip(keys, values)) is shorter when there's no change to do.

Duplicate keys do not complain

{k: v for k, v in [("a", 1), ("a", 3)]}

gives {"a": 3}. The later value overwrites the earlier one, with no error and no warning. If the input might hold duplicates and you care, that is a thing to check for, not a thing Python will tell you about.

The empty-braces trap, again

{} is an empty dict. Dicts claimed the braces long before sets existed. There's no empty-set literal at all. set() is the only way. This is worth repeating because it's the one inconsistency in an or else tidy family.

When to use them

Same rule as list comprehensions: when it fits on a line and reads as a sentence. A dict comprehension with a if-based key formula and a filter is a line you'll re-read. A loop is fine, and often kinder.

Inverting and filtering a mapping

Two jobs come up so often that the comprehension form is worth storing as an idiom rather than derived each time.

Inverting swaps keys and values:

{v: k for k, v in original.items()}

Worth pausing on: this is only lossless if the values were unique. Duplicated values collapse, and the last one processed wins, silently. If that matters, group instead of inverting.

Filtering keeps part of a mapping:

{k: v for k, v in config.items() if v is not None}

That is the standard way to drop unset options before merging configuration, and it reads better than building an empty dict and assigning into it.

Set comprehensions, and what they are for

A set comprehension is a comprehension whose result must be unique, and the two common uses are extracting a distinct field and finding a distinct derived value:

{r["city"] for r in rows}
{len(w) for w in words}

Both could be written as set([...]) around a list comprehension. The direct form avoids building the mid list, and says up front that uniqueness is the point rather than an afterthought.

When a loop is still better

The same limit applies as to list comprehensions. One change, one optional filter, one line: use the comprehension. As soon as the value needs a few steps, a try, or a rule on the key and the value, the loop version is easier to read and much easier to change later.

A worked example: a lookup table and a grouping

The two jobs that send people to a dict comprehension, side by side — because only one of them is really a comprehension:

rows = [("ana", "red"), ("bo", "blue"), ("cy", "red")]

team_of = {name: team for name, team in rows}
print(team_of["cy"])

by_team = {}
for name, team in rows:
    by_team.setdefault(team, []).append(name)
print(by_team)
red
{'red': ['ana', 'cy'], 'blue': ['bo']}

The first is a comprehension because each row makes just one entry, and later rows are allowed to overwrite earlier ones. The second is a loop because each row *adds to* an entry, and a comprehension can't do that — each iteration makes an free key and value, with no access to what the dict already holds.

That is the cutting line, and it's worth stating as a test: if the value for a key depends only on the current item, a comprehension works. If it depends on the other items too — a count, a list, a running total — you need a loop, a defaultdict, or a Counter.

Scope, and the variable that does not leak

A comprehension has its own scope. This Is why the loop variable doesn't live it:

n = "outer"
squares = [n * 2 for n in range(3)]
print(n, squares)
outer [0, 2, 4]

In Python 2 this printed 2, because the comprehension shared the enclosing scope and clobbered n. Python 3 gave comprehensions their own, which removed a whole class of accidental overwrites and is the action to lean on.

Two consequences follow. The variable is down afterwards, so a comprehension can't be used to find a thing you then want to inspect — that needs a loop. And the comprehension can still *read* names from the enclosing scope, which is what makes [x for x in items if x > threshold] work.

The one place this surprises people is inside a class body, where the comprehension's scope can't see the class's other names. A comprehension in a class body that refers to another class field raises NameError, and the fix is to find it outside the class or pass it in through the iterable.

Sets, and the operations that follow

Building a set with a comprehension is often the first half of a job whose second half is set algebra, and the two together replace a surprising amount of loop-and-flag code.

{r["city"] for r in rows} gives the distinct cities. Once you've two such sets, a - b is "in the first and not the second", a & b is "in both", and a ^ b is "in one but not both". Those three answer most of the questions people write nested loops for: which records are new, which have disappeared, which are shared.

The comprehension form matters here because it does the deduplication as it goes. Writing set([r["city"] for r in rows]) builds the whole list first and then discards the duplicates. This Is more memory for the same answer. For a large input the difference is real, and the direct form also states the intent — uniqueness is the point, not a cleanup step afterwards.

What you give up is order and indexing. A set has neither, so if the result feeds a thing that cares about sequence, sort it on the way out: sorted({...}) gives a list back in a defined order.

Nesting, and the two conditionals

Comprehensions have two places a rule can appear, and they do different things.

A trailing if filters — items that fail it give nothing at all: {k: v for k, v in d.items() if v} drops falsy values. A if-based formula in the value slot chooses. Each item makes an entry, and the rule picks what goes in it: {k: v or 0 for k, v in d.items()} keeps each key and substitutes a default. Reaching for the wrong one gives you either missing keys or unwanted ones, and the symptom is a dict of the wrong size.

Nesting a second for is legal and reads in the order written, outermost first: {c for row in grid for c in row} collects each cell of a grid of rows. The rule is that the clauses appear in the same order as the equal nested loops, which is the opposite of the order people guess when they meet it in someone else's code.

Both features are worth knowing and neither is worth mixing. A comprehension with two for clauses, a filter and a if-based value is a line that has to be decoded rather than read, and the loop it replaces would have been four clear lines.

What is built, and when it matters

The four bracket forms differ in more than the type of the result: they differ in what exists in memory while they run, and at times that is the deciding factor.

A list, set or dict comprehension builds the whole result before any of it else happens. For a thousand items that is irrelevant. For ten million it's the difference between a program that runs and one that doesn't, and the gen formula — round brackets — is the version that holds one item at a time.

The rule of thumb is what happens to the result. If it's consumed once, by a sum, a max, a for, or an any, a gen formula does the same job without materialising any of it: sum(x * 2 for x in items) never builds a list. If it's indexed, iterated twice, measured with len, or returned to a caller who will do any of those, it has to be a real collection.

There's one case where building the mid is a genuine waste and easy to miss: wrapping a list comprehension in a converter. set([f(x) for x in items]) and dict([(k, f(k)) for k in keys]) both build a list and right away throw it away. Writing the set or dict comprehension right skips that fully. This Is one reason those forms exist at all.

The reverse mistake is reaching for a gen formula where a list is wanted, and then calling list() on it. That builds the same list through more machinery, and the comprehension says it more right.

Readability, stated as a limit

each section here has hinted at a boundary, and it's worth making clear, because "use a comprehension when it's clear" is advice that gives no help at the moment of writing.

A hands-on limit: one for, at most one if, and an formula short enough that the whole thing fits on one line without wrapping. That covers the large majority of genuine uses and makes a thing a reader takes in at a glance.

Past that, the questions to ask are whether a reader could say what the result holds without tracing it, and whether it can be changed without being rewritten. A comprehension that needs a comment above it to explain what it makes has already answered both.

The escape hatch is often a named function. Pulling the formula out into a function with a real name leaves a comprehension that is one for and a call — readable again, and the complicated part now has a name, a place to put a docstring, and a place to test it.

Questions people ask about Dict and Set Comprehensions

Why does {} give a dict and not a set? Dicts had the braces first. set() is the only way to write an empty set.

Can I build a frozenset with a comprehension? Not right. frozenset(x for x in items) wraps a gen formula.

What happens to duplicate keys? The last one wins, silently. Nothing warns you.

Is a dict comprehension faster than a loop? Slightly, because the whole thing runs in one bytecode loop without repeated field lookups. Not enough to be the reason to choose it.

Can I use zip inside one? Yes, and {k: v for k, v in zip(keys, values)} is common — though dict(zip(keys, values)) is shorter when nothing is transformed.

Does the order of a dict comprehension matter? Yes. The result keeps insertion order, so the order of the input decides the order of the output.

Can I have an else without an if filter? Only in the value slot, as part of a if-based formula. The trailing filter has no else.

Can a comprehension call a function with side effects? It can, and it shouldn't. A comprehension whose result is discarded is a loop written to look like an formula.

Do comprehensions work in older Python? List comprehensions since 2.0, dict and set comprehensions since 2.7 and 3.0. any of it current supports all of them.

Dict and Set Comprehensions: recap in one screen

  • The brackets choose the type: [] list, {} set, {k: v} dict, () gen. All else about the syntax is the same.
  • A comprehension works when each item makes one free entry. Counting and grouping need a loop, setdefault or Counter.
  • Duplicate keys collapse silently, and so do duplicate set members — that is the point of a set and a hazard in a dict.
  • The loop variable has its own scope and doesn't leak, except that it can't see a class body's other names.
  • A trailing if filters items out. A if-based formula in the value slot keeps them and changes the value.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does `{n for n in [1, 2, 2]}` build?

  2. `{k: v for k, v in [('a', 1), ('a', 2)]}` gives what?

  3. How do you write an empty set comprehension result's type literal?

Cheat sheet

Dict and Set Comprehensions

Build dictionaries and sets with comprehension syntax, and see how the same loop-in-an-expression pattern works beyond lists.

PYTHON · vizlearn.in/python/dict_and_set_comprehensions.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.