Dictionary is a data structure that allows you to store values using the key-value pair. Each key-value pair maps the key with the corresponding value. In this example, you will know how to rename keys in the dictionary in python using different methods with steps.
Steps to Rename Key in Dictionary in Python
Let’s know all the steps you will follow to rename the key in the existing dictionary.
Step 1: Create a Sample Dictionary
To know all the methods you have to create a sample dictionary. It is for demonstration purposes only. You can go to step 2 to implement the methods if you want to apply to your existing dictionary.
student = {
"name": "John",
"age": 26,
"address": "123 New York"
}
print(student)
Output

Step2: Apply the below methods
After the creation of the dictionary follow all these methods to rename the key in the dictionary in python.
Method 1: Equal Operator with del keyword
The first method to rename the key in the dictionary in python is to use the square bracket to select the key and the equal operator to assign the new name. For example, if I want to rename the “name” key to “Name” then I will add the below line of code.
student["Name"] = student["name"]
del student["name"]
Full Code
student = {
"name": "John",
"age": 26,
"address": "123 New York"
}
student["Name"] = student["name"]
del student["name"]
print(student)
Output

Method 2: Rename the key in the dictionary in python using the pop function
You can also use the pop() function to rename the key in the dictionary. Here you have to pop the old key and replace it with the new key using the assignment operator(=). For example, if I want to change the “name” key to “NAME ” then I will use the below lines of code.
student["NAME"]=student.pop("name")
Full Code
student = {
"name": "John",
"age": 26,
"address": "123 New York"
}
student["NAME"]=student.pop("name")
print(student)
Output

Method 3: Using pandas dataframe
Pandas module provides rename() function to rename columns in dataframe. You can use this function. But before that, you have to first convert the dictionary to dataframe and then rename the column. After renaming you can convert the changed dataframe to dictionary using the to_dict() function.
import pandas as pd
df = pd.DataFrame(student,index=[0,1,2])
df.rename(columns={"address":"Address"},inplace=True)
print(df.to_dict())
Output

Conclusion
Sometimes dictionary may contain a typo error or a wrong key, so you have to rename the key. These are the methods to rename the key in the dictionary.
I hope you have liked this tutorial. If you have any queries or suggestions then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.