Python dictionaries don’t provide a method to rename a key.
You can rename a key in a dictionary using yourdict[“new_key”]=yourdict.pop(“old_key”) statement in Python.
Basic Example
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"fourr": 4,
}
yourdict["four"] = yourdict.pop("fourr")
yourdict
There are no direct methods to rename a dictionary key.
- Use the
pop()
method to remove the item and return the value - This value can be assigned to the new key
Output
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
This tutorial teaches you the different methods available to rename a key in a dictionary in Python.
Before using these methods, check if the old key exists in the dictionary. Otherwise, you’ll face ValueError
.
Table of Contents
Using Pop
The pop() method is used to remove an item from the dictionary and return its value.
To rename a key from a dictionary,
- Use the
pop()
method with the old key. It removes the key and returns the value of the key - Assign the returned value to a new key. To know more about adding keys to a dictionary, read: How to Add Keys to Dictionary
In other words, this is removing the existing key and assigning its value to a new key.
Since Python 3.6, the dictionaries are ordered. When you follow this approach, the item’s orders are changed. The behaviour is the same for the ordereddict
as well. The new key will be added to the end of the dictionary.
Code
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"fourr": 4,
}
yourdict["four"] = yourdict.pop("fourr")
yourdict
Output
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
This is how you can rename a key in a dictionary using the pop()
method.
Using Del Keyword
This section teaches you how to use the del
statement to remove a key from a dictionary in Python.
The del
statement is used to remove a key from a dictionary.
To use the del
statement to rename a key from a dictionary,
- Add a new key with the value from the old key using
yourdict[new_key] = yourdict[old_key]
- Delete the old key from the dictionary
Since Python 3.6, the dictionaries are ordered. When you follow this approach, the item’s orders are changed. The behaviour is the same for the ordereddict
as well. The new key will be added to the end of the dictionary.
Code
The code below demonstrates how to use the del
statement to rename a key from a dictionary. This example also renames the last dictionary item. Hence the order doesn’t change.
yourdict = {
"one": 1,
"two": 2,
"three": 3,
"fourr": 4,
}
old_key = "fourr"
yourdict["four"] = yourdict[old_key]
del yourdict[old_key]
yourdict
Output
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
Using List Comprehension (Maintain Key Order)
You can use the list comprehension method to preserve the key order while renaming a key in a dictionary.
- Iterate over the dictionary and only for a specific key, assign a new key
- For all other keys, use the same key
An if
statement is used to check for a specific key during iteration.
This method preserves the order while renaming a key in Python version 3.6 or later.
Code
yourdict = {
"one": 1,
"two": 2,
"fourr": 4,
"three": 3
}
yourdict = {"four" if k == "fourr" else k:v for k,v in yourdict.items()}
yourdict
Output
{'one': 1, 'two': 2, 'four': 4, 'three': 3}
This is how you can change the name of a dictionary key and preserve the key order in a dictionary.