How to Rename a Key in a Dictionary in Python

Python dictionaries can’t rename a key directly, so you rename a key in a dictionary by adding the value under the new name and deleting the old one. pop() does both in a single line:

d["first_name"] = d.pop("fname")

Every result here came from Python 3.12.5, with a small customer record from Austin as the data.

I’ll show the one-liner, how to keep the key’s position, renaming several keys at once, a safe helper for missing or clashing keys, nested data, and lists of records.

Rename a dictionary key with pop()

pop(old) removes the old key and hands back its value, which you store under the new key:

customer = {"id": 1042, "fname": "Emily", "city": "Austin", "balance": 249.50}

customer["first_name"] = customer.pop("fname")    # pop returns the value and removes the old key
print(customer)

Output:

{'id': 1042, 'city': 'Austin', 'balance': 249.5, 'first_name': 'Emily'}
Command Prompt output of Python dict.pop renaming the fname key to first_name
The value moved to first_name, but that key is now at the end.

Dictionaries keep insertion order, and the new key counts as a fresh insert, so it lands last. That’s fine for lookups; it matters if you print, export or compare the order.

How do you rename a key and keep its position?

If you need the order kept, build a new dictionary with a comprehension that swaps the name as it goes:

customer = {"id": 1042, "fname": "Emily", "city": "Austin", "balance": 249.50}

renamed = {("first_name" if k == "fname" else k): v for k, v in customer.items()}
print(renamed)                                    # first_name stays in second place

Output:

{'id': 1042, 'first_name': 'Emily', 'city': 'Austin', 'balance': 249.5}
Command Prompt output of a Python dictionary comprehension renaming a key while keeping it in the same position
first_name sits in second place, exactly where fname was.

This makes a new dict rather than changing the original. If other code holds a reference to the old one, reassign it or update it in place. Comprehension basics are in Python for loop in one line.

Rename multiple keys in a Python dictionary

When you need to clean up several names, put old-to-new pairs in a mapping. mapping.get(k, k) returns the new name, or the original when there isn’t one:

row = {"cust_id": 1042, "fname": "Emily", "lname": "Clark", "st": "TX", "bal": 249.50}
mapping = {"cust_id": "id", "fname": "first_name", "lname": "last_name", "st": "state", "bal": "balance"}

clean = {mapping.get(k, k): v for k, v in row.items()}   # keys not in mapping keep their name
print(clean)

Output:

{'id': 1042, 'first_name': 'Emily', 'last_name': 'Clark', 'state': 'TX', 'balance': 249.5}
Command Prompt output of Python renaming several dictionary keys at once using a mapping dictionary
Five keys renamed in one pass, with the order kept.

I use this pattern a lot when the keys come from a CSV header or an API that uses short, cryptic names.

A safe rename: missing keys and existing keys

The one-liner raises KeyError if the old key is missing, and it silently overwrites the new key if it already exists. A small helper handles both:

def rename_key(d, old, new):
    """Rename old to new in place. Returns True if something was renamed."""
    if old not in d:
        return False
    if new in d and new != old:
        raise KeyError(f"'{new}' already exists; renaming would overwrite {d[new]!r}")
    d[new] = d.pop(old)
    return True

order = {"order_id": "A-2026-118", "amt": 89.99, "total": 95.39}
print(rename_key(order, "amt", "subtotal"), order)
print(rename_key(order, "missing", "x"), order)
try:
    rename_key(order, "subtotal", "total")
except KeyError as err:
    print("KeyError:", err)

Output:

True {'order_id': 'A-2026-118', 'total': 95.39, 'subtotal': 89.99}
False {'order_id': 'A-2026-118', 'total': 95.39, 'subtotal': 89.99}
KeyError: "'total' already exists; renaming would overwrite 95.39"
Command Prompt output of a Python rename_key helper returning False for a missing key and raising KeyError instead of overwriting
Renaming subtotal to total would have wiped out 95.39, so the helper refuses.

If a missing key should just be skipped quietly, d.pop(old, None) works too. More on that error in types of errors in Python.

Rename keys in nested dictionaries

For data from JSON, keys can sit inside inner dicts and lists. A short recursive function walks the whole structure:

def rename_deep(obj, mapping):
    if isinstance(obj, dict):
        return {mapping.get(k, k): rename_deep(v, mapping) for k, v in obj.items()}
    if isinstance(obj, list):
        return [rename_deep(item, mapping) for item in obj]
    return obj

order = {"cust": {"fname": "Emily", "addr": {"zip": "78701"}},
         "items": [{"sku": "TX-100", "amt": 19.99}, {"sku": "TX-200", "amt": 5.25}]}
print(rename_deep(order, {"fname": "first_name", "amt": "price", "zip": "zip_code"}))

Output:

{'cust': {'first_name': 'Emily', 'addr': {'zip_code': '78701'}}, 'items': [{'sku': 'TX-100', 'price': 19.99}, {'sku': 'TX-200', 'price': 5.25}]}

It renames amt in both list items and zip two levels down. For more on nesting, see list of lists in Python.

Rename a key in a list of dictionaries

With records, you’ll loop and rename in each one. Changing the case of every key is just another comprehension:

orders = [{"ID": 1, "Amt": 19.99}, {"ID": 2, "Amt": 42.00}, {"ID": 3, "Amt": 7.50}]

for o in orders:
    o["amount"] = o.pop("Amt")                     # rename in every record
print(orders)

lower = [{k.lower(): v for k, v in o.items()} for o in orders]
print(lower)

Output:

[{'ID': 1, 'amount': 19.99}, {'ID': 2, 'amount': 42.0}, {'ID': 3, 'amount': 7.5}]
[{'id': 1, 'amount': 19.99}, {'id': 2, 'amount': 42.0}, {'id': 3, 'amount': 7.5}]

If you’re working with tabular data, pandas makes this simpler: df.rename(columns={"Amt": "amount"}) renames a column across every row.

Which way to rename a dictionary key?

GoalUse
Rename one key quicklyd[new] = d.pop(old)
Keep the key’s positionComprehension building a new dict
Rename many keysMapping + mapping.get(k, k)
Old key might be missingif old in d or d.pop(old, None)
Nested JSON dataRecursive function

Here are more dictionary and data guides you might like:

Frequently asked questions

How do I rename a key in a Python dictionary?

Use d[new] = d.pop(old). It stores the value under the new key and removes the old one.

Does renaming a key change the dictionary order?

With pop(), yes: the new key moves to the end. Use a comprehension to keep its position.

How do I rename multiple keys at once?

Use a mapping: {mapping.get(k, k): v for k, v in d.items()}.

What if the key doesn’t exist?

pop(old) raises KeyError. Check if old in d first, or use d.pop(old, None).

Is there a built-in rename method for dicts?

No. Python dicts have no rename method; pandas DataFrames do with rename().

Where is dict.pop documented?

See dict.pop in the Python docs.