Mapping over values in a python dictionary
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Mapping over dictionary values means applying a function to every value in a dictionary while keeping the keys unchanged. This is a fundamental data transformation operation — used for normalizing data, converting types, applying calculations, and formatting output. Python provides several approaches: dictionary comprehensions, map(), and loops.
Method 1: Dictionary Comprehension (Recommended)
The most Pythonic and readable approach:
Method 2: Using a Function
Pass a named function instead of a lambda:
Method 3: dict() with map()
Use map() with a transformation function over the items:
This is less readable than a comprehension but useful when you already have a function that transforms key-value pairs.
Method 4: In-Place Modification
Modify the dictionary directly without creating a new one:
In-place modification avoids creating a new dictionary, which matters for very large dictionaries.
Method 5: Using collections (valuesview)
For read-only transformations without creating a new dict:
Mapping with Conditional Logic
Mapping Nested Dictionary Values
Mapping Keys and Values Together
Sometimes you need to transform both:
Mapping with External Data
Performance Comparison
Dictionary comprehension is consistently the fastest approach.
Common Pitfalls
- Modifying dict during iteration:
for k in d: d[k] = ...is safe (modifying values only), butfor k in d: del d[k]raisesRuntimeError. Create a new dict or iterate over a copy of keys:for k in list(d). - Side effects in comprehension: Dictionary comprehensions should be pure transformations. Avoid putting
print(), database calls, or other side effects inside comprehensions — use a loop instead. - Forgetting .items():
{k: v*2 for k, v in d}fails because iterating over a dict yields keys only. Use.items()to get key-value pairs. - Type errors: If values have mixed types, the transformation function may fail on some values. Use
try/exceptor check types:{k: int(v) if isinstance(v, str) else v for k, v in d.items()}. - Large dictionaries: Comprehensions create a new dictionary in memory. For very large dicts where you only need to iterate once, use a generator expression instead of materializing the result.
Summary
- Use
{k: f(v) for k, v in d.items()}for the fastest, most readable value transformation - Use
dict(map(lambda item: (item[0], f(item[1])), d.items()))if you already have a transformation function - Modify in place with
for k in d: d[k] = f(d[k])to avoid creating a new dictionary - Add
ifconditions for conditional mapping:{k: f(v) for k, v in d.items() if condition(v)} - Dictionary comprehension is faster than both
map()and explicit loops
Related reading
- Mark task as completed
- MassTransit with RabbitMQ recovering the error queue
- MassTransit with RabbitMQ When is a message moved to the error queue
- Matching array entries in AWS Cloudwatch log metrics filter
- Matplotlib different size subplots
- Matplotlib draw grid lines behind other graph elements
- Matrix arrangement issues in php
- matrix multiplication algorithm time complexity

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.