Rename a dictionary key
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
Python dictionaries do not have a built-in “rename key” operation because a key is part of the hash-table identity. In practice, renaming means creating the new key and removing the old one. The best approach depends on whether you are changing one key, many keys, or nested data structures.
Rename One Key with pop
The most common pattern is to copy the value to a new key and remove the old one in one expression.
pop("email") returns the old value and removes the key from the dictionary. This is the cleanest one-key rename when you know the source key exists.
Handle Missing Keys Safely
If the old key may not be present, check first or provide a default. This prevents unexpected KeyError.
If you want a single expression and can tolerate a sentinel default:
Be careful if None is also a valid real value in your data.
Renaming Multiple Keys at Once
For more than one rename, a new dictionary is often clearer than mutating in place repeatedly.
This pattern scales well for schema transformations and API adaptation.
Preserve Order and Intent
Modern Python preserves insertion order in dictionaries, so renaming can subtly affect output order depending on how you do it. If order matters, a dictionary comprehension or ordered reconstruction is often easier to reason about than repeated mutation.
For example, user["contact"] = user.pop("email") moves the renamed key to the end. If that matters for output or tests, rebuild the dictionary in the desired order instead of mutating in place.
Renaming Keys in Nested Structures
Real data often contains nested dictionaries and lists. In those cases, write a helper that traverses recursively.
This is useful when transforming JSON-like payloads between internal and external schemas.
When Not to Rename In Place
If other code still expects the old key, in-place renaming can introduce bugs that are hard to trace. In larger systems, prefer creating a transformed dictionary at boundaries instead of mutating shared input objects. This is especially important in request handlers, serializers, and data-cleaning pipelines.
Common Pitfalls
- Using
popwithout handling missing keys. - Renaming in place when other code still depends on the original key.
- Forgetting that in-place rename can change dictionary insertion order.
- Mutating nested data manually in many places instead of using one helper.
- Reusing one rename strategy when a clean rebuilt dictionary would be clearer.
Summary
- Python renames dictionary keys by creating a new key and removing the old one.
- For one key,
new_key = d.pop(old_key)is the usual pattern. - For multiple renames, rebuilding the dictionary is often cleaner.
- Use recursive helpers for nested JSON-like structures.
- Prefer transformation at system boundaries over mutating shared dictionaries in place.
Related reading
- Reorder vector using a vector of indices
- Reordering a list to maximize difference of adjacent elements
- Reordering of array elements
- Repeatedly removing the maximum average subarray
- Rename multiple files in a directory in Python
- Rename Pandas DataFrame Index
- Replace a character at a specific index in a string?
- Replace all elements of NumPy array that are greater than some value

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.