How do you use dict comprehensions effectively, and what are common patterns in data work?
A dict comprehension uses the syntax `{key_expr: value_expr for item in iterable if condition}` to build a dictionary in one expression. It is faster than calling `dict()` on a list of pairs and more readable than a for-loop with repeated `d[k] = v` assignments. Common data patterns include inverting a dict, grouping values, and transforming keys or values in bulk.
How to think about it
Dict comprehensions are the natural extension of list comprehensions to key-value pairs. The pattern is {key: value for item in iterable if condition} — same filtering and transformation power, but you get a dict back instead of a list. They show up constantly in data work: building lookup tables, normalising messy keys, inverting mappings.
What’s really being tested
Interviewers want to see that you can read and write the syntax fluently, know the common data patterns, and understand the one silent gotcha: duplicate values when inverting a dict.
Step 1 — Basic syntax
The simplest form transforms a range into a dict of squares:
squares = {x: x**2 for x in range(10) if x % 2 == 0}
# {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
Step 2 — The four patterns you’ll use constantly
- Invert a dict (swap keys and values)
- Normalise keys (strip whitespace, lowercase)
- Build a lookup table from a list of records
- Filter a dict by value
Step 3 — Performance note
Dict comprehensions are faster than dict() on a list of tuples because they avoid constructing an intermediate iterator of tuples.
The key insight — comprehensions vs loops
A dict comprehension is not just syntactic sugar — it runs faster because the Python interpreter optimises it into a single bytecode instruction (BUILD_MAP) rather than a series of STORE_SUBSCR calls inside a loop. For large transformations (millions of records), this matters.