How To Rename A Key In a Dictionary Python – Definitive Guide

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.

This tutorial teaches you the different methods available to rename a key in a dictionary in Python.

If You’re in Hurry…

There are no direct methods to rename a dictionary key.

You can use the pop() method to rename a key in a dictionary. It removes the item and returns the value of the removed key that can be assigned to the new key. This acts as a renaming of a key in 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}

If You Want to Understand Details, Read on…

Different methods are available to rename a dictionary key in python though the dictionary object provides NO direct methods.

Let us learn each method in detail.

Before using these methods, check if the old key exists in the dictionary. Otherwise, you’ll face ValueError.

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

The code below shows how the pop() method can remove a key from a dictionary. Since the example code removes the last item in the dictionary, the order doesn’t seem to change.

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.

This involves iterating over the dictionary and only for a specific key a new key is used, and for all other keys, the same key values are used.

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

The code below demonstrates how to use the list comprehension to rename a key and preserve the order of the key.

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.

Conclusion

There are no direct methods available. However, you can change the name of the dictionary key in python using the pop() method and the del statement.

You’ve also learned how to preserve the key order while renaming a key in a dictionary in Python.

You May Also Like

Leave a Comment