Rename key in a python dictionary

Rename a Key in a Python Dictionary

When working with dictionaries it might happen that you need to modify the key name without changing its value for a particular key-value pair. In this tutorial, we will look at how to rename a key in a Python dictionary with the help of some examples.

Can we rename a key in a dictionary in Python?

There is no direct way of renaming a key in a Python dictionary. But, you can achieve the end result using the following steps –

  1. Remove the old key to value pair from the dictionary.
  2. Add the new key to value pair to the dictionary.

The order of the above steps is not important so long as you use the same value for the new key.

How to rename a key in a Python dictionary?

To rename a Python dictionary key, use the dictionary pop() function to remove the old key from the dictionary and return its value. And then add the new key with the same value to the dictionary.

The following is the syntax –

# rename key in dictionary
my_dict[new_key] = my_dict.pop(old_key)

Let’s now look at some examples of using the above syntax.

We have a dictionary containing countries to their capital cities mapping. Let’s rename the key “USA” to “United States of America”.

# create a dictionary
capital_info = {
    "India": "New Delhi",
    "USA": "Washington DC",
    "UK": "London"
}
# rename key in dictionary
capital_info["United States of America"] = capital_info.pop("USA")
# display the dictionary
print(capital_info)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

{'India': 'New Delhi', 'UK': 'London', 'United States of America': 'Washington DC'}

Here we are removing the old key, “USA” and returning its value using the pop() function and then assigning the returned value to the new key, “United States of America”. You can see that instead of “USA” we have “United States of America” in the dictionary.

Let’s look at another example.

# rename key in dictionary
capital_info["United Kingdom"] = capital_info.pop("UK")
# display the dictionary
print(capital_info)

Output:

{'India': 'New Delhi', 'United States of America': 'Washington DC', 'United Kingdom': 'London'}

Here we rename “UK” to “United Kingdom”.

Note that we’re not really renaming here. We are removing the old key and then adding the new key with the same value thereby getting the same end result as a rename operation (if it were available).

Using del keyword

Alternatively, you can use the del keyword instead of the pop() function to remove a key from a dictionary. Check out this tutorial on the differences between the two.

# create a dictionary
capital_info = {
    "India": "New Delhi",
    "USA": "Washington DC",
    "UK": "London"
}
# rename key in dictionary
capital_info["United States of America"] = capital_info["USA"]
del capital_info["USA"]
# display the dictionary
print(capital_info)

Output:

{'India': 'New Delhi', 'UK': 'London', 'United States of America': 'Washington DC'}

We get the same result as above.

Note that if the key is not present in the dictionary, removing using the del keyword will result in a KeyError.

You might also be interested in –

  1. Get Value for a Key in a Python Dictionary
  2. Python – Count Keys in a Dictionary
  3. Check If a Python Dictionary Contains a Specific Key


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top