Python - Change type of key in Dictionary list - GeeksforGeeks

10 May, 2020
Sometimes, while working with data, we can have a problem in which we require to change the type of particular keys’ value in list of dictionary. This kind of problem can have application in data domains. Lets discuss certain ways in which this task can be performed.
Input : test_list = [{‘gfg’: 9, ‘best’: (7, 2), ‘is’: ‘4’}, {‘is’: ‘2’}]Output : [{‘gfg’: 9, ‘best’: (7, 2), ‘is’: 4}, {‘is’: 2}]
Input : test_list = [{‘is’ : ‘98393’}]Output : [{‘is’ : 98393}]
Method #1 : Using loopThis is brute force way to approach this problem. In this, we iterate for all list elements and dictionary keys and convert the desired dictionary key’s value to required type.
# Python3 code to demonstrate working of # Change type of key in Dictionary list# Using loop  # initializing listtest_list = [{'gfg' : 1, 'is' : '56', 'best' : (1, 2)},             {'gfg' : 5, 'is' : '12', 'best' : (6, 2)},            {'gfg' : 3, 'is' : '789', 'best' : (7, 2)}]  # printing original listprint("The original list is : " + str(test_list))  # initializing change key chnge_key = 'is'  # Change type of key in Dictionary list# Using loopfor sub in test_list:        sub[chnge_key] = int(sub[chnge_key])  # printing result print("The converted Dictionary list : " + str(test_list)) 
The original list is : [{‘is’: ’56’, ‘gfg’: 1, ‘best’: (1, 2)}, {‘is’: ’12’, ‘gfg’: 5, ‘best’: (6, 2)}, {‘is’: ‘789’, ‘gfg’: 3, ‘best’: (7, 2)}]The converted Dictionary list : [{‘is’: 56, ‘gfg’: 1, ‘best’: (1, 2)}, {‘is’: 12, ‘gfg’: 5, ‘best’: (6, 2)}, {‘is’: 789, ‘gfg’: 3, ‘best’: (7, 2)}]
 
Method #2 : Using list comprehensionThis task can also be performed using list comprehension as a shorthand. In this, we iterate for list in shorter way as above with same approach.
# Python3 code to demonstrate working of # Change type of key in Dictionary list# Using list comprehension  # initializing listtest_list = [{'gfg' : 1, 'is' : '56', 'best' : (1, 2)},             {'gfg' : 5, 'is' : '12', 'best' : (6, 2)},            {'gfg' : 3, 'is' : '789', 'best' : (7, 2)}]  # printing original listprint("The original list is : " + str(test_list))  # initializing change key chnge_key = 'is'  # Change type of key in Dictionary list# Using list comprehensionres = [{key : (int(val) if key == chnge_key else val)                        for key, val in sub.items()}                        for sub in test_list]  # printing result print("The converted Dictionary list : " + str(res)) 
The original list is : [{‘is’: ’56’, ‘gfg’: 1, ‘best’: (1, 2)}, {‘is’: ’12’, ‘gfg’: 5, ‘best’: (6, 2)}, {‘is’: ‘789’, ‘gfg’: 3, ‘best’: (7, 2)}]The converted Dictionary list : [{‘is’: 56, ‘gfg’: 1, ‘best’: (1, 2)}, {‘is’: 12, ‘gfg’: 5, ‘best’: (6, 2)}, {‘is’: 789, ‘gfg’: 3, ‘best’: (7, 2)}]
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?