HOWTO · Python

How to Change the Key in a Dictionary in Python

Learn how to rename one or more keys in a Python dictionary while keeping their values.

On this page

To rename a key in a Python dictionary, assign its value to the replacement key and then remove the old key. Use dict.pop() when you want to retrieve and remove the old entry in one operation. For several or conditional changes, build a new dictionary from a mapping of old keys to new ones. These approaches keep the associated values; choose based on whether you want to update the original dictionary or create a separate result.

Change a key by assigning its value to a new key and deleting the old key

A dictionary key is not edited in place. For a simple rename, read the value using the old key, assign it under the new key, and delete the old entry:

fruit = {"A": "Apple", "B": "Banana"}
fruit["C"] = fruit["B"]
del fruit["B"]
print(fruit)

This changes B to C while keeping the value "Banana". The operation updates fruit itself. If C already exists, assigning to it replaces its current value, so check that the destination is available when that matters to your data.

Change a key with dict.pop()

dict.pop() removes the specified key and returns its value. You can pass that value directly to the replacement key:

person = {"first_name": "John", "last_name": "Doe", "age": 30}
person["given_name"] = person.pop("first_name")

After the assignment, person has given_name with the former first_name value. As with direct assignment, an existing given_name value is replaced. If the old key may be absent, supply a default to pop, such as person.pop("first_name", None); otherwise, calling it for a missing key raises KeyError.

Change one or more keys by rebuilding a dictionary

For multiple or conditional renames, map each old key to its replacement and use a dictionary comprehension. Keys not listed in the mapping stay the same:

original = {"a": 1, "b": 2, "c": 3}
key_mapping = {"a": "alpha", "b": "beta"}
renamed = {key_mapping.get(key, key): value for key, value in original.items()}

Here, renamed contains alpha, beta, and c with their original values. The comprehension creates a new dictionary, so original remains available without those changes. If two source keys map to the same destination key, the later value assigned during the comprehension replaces the earlier one. Ensure the mapping produces distinct keys if every value must be retained. This is especially useful when a rename depends on a rule applied to every key or when several known names need to change together. Review the mapping before rebuilding if the original data contains keys that could converge on the same name.

This method also works for conditional transformations: replace key_mapping.get(key, key) with an expression that returns the desired key for each item. The key expression should be deterministic for the data being processed, and the resulting keys must be hashable, as dictionary keys require.

Rename DataFrame columns when dictionary keys represent tabular data

When dictionary keys are column names in a tabular dataset, use DataFrame.rename(columns=...) to rename columns. Creating a DataFrame from a dictionary does not rename keys in the original dictionary; it creates a separate tabular object. For example, the operation can be written as df = df.rename(columns={"a": "alpha"}). This returns a DataFrame with the column renamed and leaves the source dictionary unchanged. If you need a dictionary from the result, choose an explicit orientation such as df.to_dict(orient="list") when the values are column lists. This Pandas-specific operation requires Pandas to be available in your environment. For ordinary dictionaries, the built-in assignment, dict.pop(), or comprehension approaches avoid converting the data structure and are usually the simpler choice.