How to change key in dictionary in Python
This article will let you know how to change the key of a dictionary in python even though the key is immutable.
Dictionary is a kind of array of key-value pairs. If you have to access any value then you need its key to get it. Here the values or data are directly mapped to its key. All keys must be unique so that is can search its mapped value without any data redundancy. Also, keys are immutable which arise a big question that how you can change key in the dictionary. Let’s find them
Poping the pair
pop() is an inbuilt function in the dictionary return the value of that particular key. And you can use them to create a new element with a new name.
scoreDict = {'Sachin' : 23, 'Rahane' : 54, 'Dhawan' : 0, 'Rohit' : 110} print('Before change',str(scoreDict)) #Changing the 'Dhawan' key to 'Rahul' key scoreDict['Rahul'] = scoreDict.pop('Dhawan') print('After change',str(scoreDict))
Output:
Before change {‘Sachin’ : 23, ‘Rahane’ : 54, ‘Dhawan’ : 0, ‘Rohit’ : 110}
After change {‘Sachin’ : 23, ‘Rahane’ : 54, ‘Rohit’ : 110, ‘Rahul’ : 0}
Copy and Delete
By copying the required value to the new key and deleting it using del command is the easiest way to change the key. Since the keys are immutable but you can use this trick to do this.
scoreDict = {'Sachin' : 23, 'Rahane' : 54, 'Dhawan' : 0, 'Rohit' : 110} print('Before change',str(scoreDict)) #Changing the 'Dhawan' key to 'Rahul' key scoreDict['Rahul'] = scoreDict['Dhawan'] del scoreDict['Dhawan'] print('After change',str(scoreDict))
Output:
Before change {‘Sachin’ : 23, ‘Rahane’ : 54, ‘Dhawan’ : 0, ‘Rohit’ : 110}
After change {‘Sachin’ : 23, ‘Rahane’ : 54, ‘Rohit’ : 110, ‘Rahul’ : 0}
Here is another example: change key in dictionary in Python
Dictionary is a sequence of ( Key: Value ) pair.
Certainly, it is possible to change the keys in the Dictionary
Let’s just create a sample Dictionary.
Initial Dictionary :
sample = { 'bhanu' : 438 , 'surya' : 441 , 'jagan' : 427 } print (sample)
Output : {'bhanu': 438, 'surya': 441, 'jagan': 427}
Let’s see the various methods to change the keys in the dictionary.
Change Keys of Dictionary in Python
First method:
This approach is to simply create a new key with an existing value.
sample = { 'bhanu' : 438 , 'surya' : 441 , 'jagan' : 427 } print (sample) print() # Method 1 sample ['varshita'] = sample ['surya'] # new key "varshita" created with existing value del sample ['surya'] print (sample)
Output : {'bhanu': 438, 'jagan': 427, 'surya': 441} {'bhanu': 438, 'jagan': 427, 'varshita': 441}
Second method:
Using pop( ) built-in method of dictionary.
Before proceeding, let’s understand how it works.
sample = { 'bhanu' : 438 , 'surya' : 441 , 'jagan' : 427 } # Method 2 result = sample.pop('surya') # the return of this method is the value of sample['surya']. print (result) print (sample) # prints the dictionary excluding the key 'surya' as its removed.
Output : 441 {'bhanu': 438, 'jagan': 427}
Now, using this property to change the keys in the dictionary.
sample = { 'bhanu' : 438 , 'surya' : 441 , 'jagan' : 427 } # Method 2 sample ['sairam'] = sample.pop('surya') # adds key 'sairam' with a value of 441 [as 'surya ': 441] print (sample)
Output : {'sairam': 441, 'jagan': 427, 'bhanu': 438}
Concluding this, these are different ways to change the keys in a dictionary and can be helpful to do further operations.
Leave a Reply