How to Change a Key in a Dictionary in Python?

As a Python developer working on a project for a US-based client, I recently encountered a situation where I needed to rename a key in a dictionary without modifying its associated value. After researching various methods, I discovered several effective approaches to achieve this task. In this tutorial, I will explain how to change a key in a dictionary in Python with examples.

Change a Key in a Dictionary in Python

Let’s consider a real-world scenario. Suppose you are working with a dictionary that stores information about US states and their capitals. The dictionary uses state abbreviations as keys and capital names as values. For example:

state_capitals = {
    'CA': 'Sacramento',
    'NY': 'Albany',
    'TX': 'Austin'
}

Now, imagine you need to change the key ‘CA’ to ‘California’ without losing its associated value. How can you accomplish this?

Read How to Concat Dict in Python?

Method 1: Add a New Key and Remove the Old Key

One simple approach to changing a key in a Python dictionary is to add a new item and then remove the old key. Here’s how you can do it:

state_capitals['California'] = state_capitals.pop('CA')

In this example, we use the pop() method to retrieve the value associated with the key ‘CA’ and simultaneously remove the key-value pair from the dictionary. We then assign the retrieved value to a new key ‘California’.

After executing this code, the state_capitals dictionary will look like this:

{
    'NY': 'Albany',
    'TX': 'Austin',
    'California': 'Sacramento'
}

You can see the output in the screenshot below.

Change a Key in a Dictionary in Python

Check out Write a Python Program to Remove Duplicates From a Dictionary

Method 2: Use the dict() Constructor

Another way to change a key in a dictionary is by creating a new dictionary using the dict() constructor and a generator expression. Here’s an example:

state_capitals = dict((('California', value) if key == 'CA' else (key, value) for key, value in state_capitals.items()))

In this approach, we use a generator expression to iterate over the key-value pairs of the state_capitals dictionary. For each pair, we check if the key is ‘CA’. If it is, we create a new tuple with the key ‘California’ and the corresponding value. Otherwise, we keep the key-value pair unchanged. Finally, we pass the generated tuples to the dict() constructor to create a new dictionary with the updated key.

You can see the output in the screenshot below.

How to Change a Key in a Dictionary in Python

Read How to Search in Dictionary By Value in Python

Method 3: Use Dictionary Comprehension

Python’s dictionary comprehension allows us to create a new dictionary while modifying its keys. Here’s how you can use it to change a key:

state_capitals = {('California' if key == 'CA' else key): value for key, value in state_capitals.items()}

Similar to the previous method, we use a dictionary comprehension to iterate over the key-value pairs of the state_capitals dictionary. For each pair, we check if the key is ‘CA’ and replace it with ‘California’ if true, otherwise, we keep the original key. The resulting key-value pairs are used to create a new dictionary.

You can see the output in the screenshot below.

Change a Key in a Dictionary in Python Dictionary Comprehension

Read How to Sum All Values in a Python Dictionary?

Conclusion

In this tutorial, I helped you to learn how to change a key in a dictionary in Python. I explained mainly three methods to accomplish this task, such as adding a new key and removing the old key, using the dict() constructor, and using dictionary comprehension.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.