Python | Transcribing dictionary key

While working with data, we can come across various data interconversions that we wish to perform. These can be of transcribing a key inside a list of dictionary to the outer part for generalizations. This type of utility is quite useful to know. Let’s discuss certain ways in which this can be performed.

Method #1 : Using dictionary comprehension

This particular task can be performed using the method of dictionary comprehension in which we assign the keys and values from the desired lists and remake the dictionary.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Dictionary key transcription
# dictionary comprehension
  
# initializing list
test_list = [{'state' : 'Haryana', 'capital' : 'Chandigarh', 'area' : 'North'},
             {'state' : 'Karnataka', 'capital' : 'Bengaluru', 'area' : 'South'}]
  
# printing original list
print("The original list : " + str(test_list))
  
# using Dictionary comprehension
# Dictionary key transcription
res = { sub["state"]: {"capital": sub["capital"], "area": sub["area"] }
        for sub in test_list }
  
# print result
print("The Dictionary after transcription of key : " + str(res))

chevron_right


Output :

The original list : [{‘capital’: ‘Chandigarh’, ‘state’: ‘Haryana’, ‘area’: ‘North’}, {‘capital’: ‘Bengaluru’, ‘state’: ‘Karnataka’, ‘area’: ‘South’}]

The Dictionary after transcription of key : {‘Haryana’: {‘capital’: ‘Chandigarh’, ‘area’: ‘North’}, ‘Karnataka’: {‘capital’: ‘Bengaluru’, ‘area’: ‘South’}}

 

Method #2 : Using dictionary comprehension + items() + get()

This task can also be performed using a set of functions as well. This method allows the flexibility of adding any key of choice. This is useful in cases where keys are not known in advance.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Dictionary key transcription
# dictionary comprehension + items() + get()
  
# initializing list
test_list = [{'state' : 'Haryana', 'capital' : 'Chandigarh', 'area' : 'North'},
             {'state' : 'Karnataka', 'capital' : 'Bengaluru', 'area' : 'South'}]
  
# printing original list
print("The original list : " + str(test_list))
  
# using dictionary comprehension + items() + get()
# Dictionary key transcription
res = {sub.get('state'): {key: val for key, val in sub.items()
        if key != 'state'} for sub in test_list}
  
# print result
print("The Dictionary after transcription of key : " + str(res))

chevron_right


Output :

The original list : [{‘capital’: ‘Chandigarh’, ‘state’: ‘Haryana’, ‘area’: ‘North’}, {‘capital’: ‘Bengaluru’, ‘state’: ‘Karnataka’, ‘area’: ‘South’}]

The Dictionary after transcription of key : {‘Haryana’: {‘capital’: ‘Chandigarh’, ‘area’: ‘North’}, ‘Karnataka’: {‘capital’: ‘Bengaluru’, ‘area’: ‘South’}}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.