Python | Type conversion in dictionary values
Last Updated :
27 Apr, 2023
The problem of conventional type conversion is quite common and can be easily done using the built-in converters of python libraries. But sometimes, we may require the same functionality in a more complex scenario vis. for keys of list of dictionaries. Let’s discuss certain ways in which this can be achieved.
Method #1 : Naive Method
In the naive method, we employ 2 loops, nested. One for all the dictionaries in the list and the second one for the dictionary key-value pairs in a specific dictionary.
Python3
test_list = [{ 'a' : '1' , 'b' : '2' }, { 'c' : '3' , 'd' : '4' }]
print ( "The original list is : " + str (test_list))
for dicts in test_list:
for keys in dicts:
dicts[keys] = int (dicts[keys])
print ( "The modified converted list is : " + str (test_list))
|
Output :
The original list is : [{'a': '1', 'b': '2'}, {'c': '3', 'd': '4'}]
The modified converted list is : [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
Time complexity: O(n*n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Method #2 : Using items() + list comprehension
This can easily performed using just a one line with the help of list comprehension. The items function can be exploited to extract the list values as when required and list comprehension part handles the iteration part.
Python3
test_list = [{ 'a' : '1' , 'b' : '2' }, { 'c' : '3' , 'd' : '4' }]
print ( "The original list is : " + str (test_list))
res = [ dict ([key, int (value)]
for key, value in dicts.items())
for dicts in test_list]
print ( "The modified converted list is : " + str (res))
|
Output :
The original list is : [{'b': '2', 'a': '1'}, {'c': '3', 'd': '4'}]
The modified converted list is : [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
Method #3 : Using map() function and a lambda
Another approach that you can use is to use a combination of the map() function and a lambda function to perform the type conversion on the dictionary values.
Here is an example of how this can be done:
This code converts the values in a list of dictionaries from strings to integers. It does this by using the map() function to apply a lambda function to each dictionary in the list. The lambda function creates a new dictionary with the same keys as the original dictionary, but the values are converted to integers using the int() function.
The time complexity of this code is O(n), where n is the number of dictionaries in the list. This is because the code iterates over each dictionary in the list once.
The space complexity of this code is also O(n), because the code creates a new dictionary for each dictionary in the list.
Python3
test_list = [{ 'a' : '1' , 'b' : '2' }, { 'c' : '3' , 'd' : '4' }]
result = [{k: int (v) for k, v in d.items()} for d in map ( lambda x: dict (x), test_list)]
print (result)
|
Output
[{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
Share your thoughts in the comments
Please Login to comment...