Python | Ways to change keys in dictionary

Given a dictionary, the task is to change the key based on the requirement. Let’s see different methods we can do this task.

Method #1 : Using naive method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# changing keys of dictionary
# using naive method
  
# inititialising dictionary
ini_dict = {'nikhil': 1, 'vashu' : 5,
            'manjeet' : 10, 'akshat' : 15}
  
# printing initial json
print ("initial 1st dictionary", ini_dict)
  
# changing keys of dictionary
ini_dict['akash'] = ini_dict['akshat']
del ini_dict['akshat']
  
  
# printing final result
print ("final dictionary", str(ini_dict))

chevron_right


Output:



initial 1st dictionary {‘akshat’: 15, ‘nikhil’: 1, ‘manjeet’: 10, ‘vashu’: 5}
final dictionary {‘akash’: 15, ‘nikhil’: 1, ‘manjeet’: 10, ‘vashu’: 5}

 
Method #2: Using pop()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# changing keys of dictionary
# using pop() method
  
# inititialising dictionary
ini_dict = {'nikhil': 1, 'vashu' : 5
            'manjeet' : 10, 'akshat' : 15}
  
# printing initial json
print ("initial 1st dictionary", ini_dict)
  
# changing keys of dictionary
ini_dict['akash'] = ini_dict.pop('akshat')
  
# printing final result
print ("final dictionary", str(ini_dict))

chevron_right


Output:

initial 1st dictionary {‘akshat’: 15, ‘manjeet’: 10, ‘vashu’: 5, ‘nikhil’: 1}
final dictionary {‘akash’: 15, ‘manjeet’: 10, ‘vashu’: 5, ‘nikhil’: 1}

 
Method #3: Using zip()

Suppose we want to change all keys of dictionary.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# changing all keys of dictionary
# corresponding to list using zip()
  
# inititialising dictionary
ini_dict = {'nikhil': 1, 'vashu' : 5
            'manjeet' : 10, 'akshat' : 15}
  
# initialising list
ini_list = ['a', 'b', 'c', 'd']
  
# printing initial json
print ("initial 1st dictionary", ini_dict)
  
# changing keys of dictionary
final_dict = dict(zip(ini_list, list(ini_dict.values())))
  
# printing final result
print ("final dictionary", str(final_dict))

chevron_right


Output:

initial 1st dictionary {‘akshat’: 15, ‘manjeet’: 10, ‘vashu’: 5, ‘nikhil’: 1}
final dictionary {‘c’: 5, ‘d’: 1, ‘a’: 15, ‘b’: 10}



My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.