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 –
- Remove the old key to value pair from the dictionary.
- 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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- Get Value for a Key in a Python Dictionary
- Python – Count Keys in a Dictionary
- 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.