Dictionary Comprehension Guide¶
Zero-dependency Python snippets for dictionary comprehensions using the standard library.
16 snippets available in this sub-category.
Simple¶
Basic dictionary comprehension¶
dict
comprehension
basic
data-structures
Create a dict from an iterable
Notes
- Most concise way to build a dict from an iterable
From list of tuples¶
dict
comprehension
tuple
data-structures
Build dict from list of (key, value) tuples
Notes
- Each tuple must have exactly two elements (key, value)
From two lists (zip)¶
dict
comprehension
zip
data-structures
Build dict from zipped lists
keys = ["a", "b", "c"]
values = [1, 2, 3]
d = {k: v for k, v in zip(keys, values)}
print(d) # {'a': 1, 'b': 2, 'c': 3}
Notes
- Stops at shortest list if lengths differ
Filtering and Transforming¶
Filter items by value¶
dict
comprehension
filter
data-structures
Filter dict by value
d = {"a": 1, "b": 2, "c": 3}
even = {k: v for k, v in d.items() if v % 2 == 0}
print(even) # {'b': 2}
Notes
- Only includes items where value matches condition
Transform keys and values¶
dict
comprehension
transform
data-structures
Transform keys and values in comprehension
d = {"a": 1, "b": 2}
transformed = {k.upper(): v * 10 for k, v in d.items()}
print(transformed) # {'A': 10, 'B': 20}
Notes
- Can change both keys and values in one step
Conditional values¶
dict
comprehension
conditional
data-structures
Use conditionals in value expression
nums = [1, 2, 3, 4]
parity = {x: ("even" if x % 2 == 0 else "odd") for x in nums}
print(parity) # {1: 'odd', 2: 'even', 3: 'odd', 4: 'even'}
Notes
- Ternary operator allows inline if/else for values
Advanced Patterns¶
Nested dictionary comprehension¶
dict
comprehension
nested
data-structures
Build nested dicts with comprehensions
matrix = [[1, 2], [3, 4]]
d = {i: {j: val for j, val in enumerate(row)} for i, row in enumerate(matrix)}
print(d) # {0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}
Notes
- Useful for matrices or multi-level data
Flatten nested dict to single dict¶
dict
comprehension
flatten
data-structures
Flatten nested dicts with comprehension
nested = {"a": {"x": 1}, "b": {"y": 2}}
flat = {f"{k}_{ik}": iv for k, v in nested.items() for ik, iv in v.items()}
print(flat) # {'a_x': 1, 'b_y': 2}
Notes
- Key names are combined for uniqueness
Enumerate with comprehension¶
dict
comprehension
enumerate
data-structures
Use enumerate in dict comprehension
lst = ["apple", "banana"]
d = {i: v for i, v in enumerate(lst)}
print(d) # {0: 'apple', 1: 'banana'}
Notes
- Indexes become keys
Default values with get()¶
dict
comprehension
default
get
data-structures
Set default value if key missing
keys = ["a", "b", "c"]
source = {"a": 1, "c": 3}
d = {k: source.get(k, 0) for k in keys}
print(d) # {'a': 1, 'b': 0, 'c': 3}
Notes
- get() provides fallback for missing keys
Comprehension with try/except (error handling)¶
dict
comprehension
error
try-except
data-structures
Handle errors in dict building (not possible in pure comprehension)
lst = ["1", "2", "x"]
d = {}
for s in lst:
try:
d[s] = int(s)
except ValueError:
d[s] = None
print(d) # {'1': 1, '2': 2, 'x': None}
Notes
- Use a for loop for error handling (comprehensions can't catch exceptions)
Dictionary comprehension from set¶
dict
comprehension
set
data-structures
Build dict from set
Notes
- Set elements become keys
Dictionary comprehension with filtering and transformation¶
dict
comprehension
filter
transform
data-structures
Combine filtering and transformation
nums = range(10)
squares_even = {x: x * x for x in nums if x % 2 == 0}
print(squares_even) # {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
Notes
- Only even numbers included as keys
Performance and Idioms¶
Large dict comprehensions (performance)¶
dict
comprehension
performance
data-structures
Dict comprehensions are fast and memory efficient
import time
N = 10**6
start = time.time()
d = {x: x * 2 for x in range(N)}
print("Time:", time.time() - start)
Notes
- Comprehensions are faster than for-loops for building dicts
Avoiding key collisions¶
dict
comprehension
collision
data-structures
Last occurrence wins for duplicate keys
Notes
- Later values overwrite earlier ones for duplicate keys
Edge cases: empty input, non-hashable keys¶
dict
comprehension
edge-case
data-structures
Handle edge cases for dict comprehensions
print({k: v for k, v in []}) # {}
try:
d = {[1, 2]: "x" for _ in range(1)}
except TypeError as e:
print(e) # unhashable type: 'list'
Notes
- Keys must be hashable; empty input returns empty dict
🔗 Cross Reference¶
- Reference: See 📂 Sort Dictionary
- Reference: See 📂 Group by Key
🏷️ Tags¶
dict
, comprehension
, filter
, transform
, nested
, flatten
, enumerate
, zip
, default
, performance
, edge-case
, data-structures
📝 Notes¶
- Dict comprehensions are powerful for building, filtering, and transforming dicts
- Use for-loops for error handling or more complex logic