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.
# 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)) |
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.
# 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)) |
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’}}
Recommended Posts:
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python Dictionary
- Python | Get key from value in Dictionary
- Python | Dictionary with index as value
- Python | Nested Dictionary
- Python | Dictionary has_key()
- Python Dictionary copy()
- Python | Key index in Dictionary
- Python | Get key with maximum value in Dictionary
- Python Dictionary Comprehension
- Python | Add new keys to a dictionary
- Python | Convert a set into dictionary
- Python dictionary | values()
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.



