Dictionary | How To Change The Name Of A Key In Dictionary?

In this tutorial, we will learn How to Change the name of a key in the dictionary where the dictionary stores the data in the form of key value pair and to rename we simply use pop() to rename the old name with a new name.

Dictionary

Dictionary In Python

A dictionary in python is a data structure that holds the data in the form of key value pairs we describe a key along with a value that we want to store for example if we want to store the name of students then in general we just create a list or array to store just names of all the students.

But in the case of the dictionary, it may hold the data related to particular students then we may store the data like name:’ram’ class: ‘cse’ where we can have a key, and corresponding to the key we may have their values which are stored.

Rename The Key Of Dictionary

There is a way by which we can rename the certain key stored in the dictionary to the name we want for example if we want to rename the name to the first name so we can make it easy for us to access and increase the readability.

To do the things as per your choice follow the code given below.

d = {'old_key': 'value'}
d['new_key'] = d.pop('old_key')
print(d)  # Output: {'new_key': 'value'

Here we have a dictionary d with a key as an old key and in this code, we rename the old key with a new key by simply passing the key as an argument.

 

To learn more about dictionary operations at  Dictionary operations to rename the key.

To learn more about python visit How do I Reverse a List backward? | Python Tutorial.  and also visit beta python for programming Solutions to learn more about different programming languages.

Leave a Comment

%d bloggers like this: