Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Type conversion in dictionary values

Python | Type conversion in dictionary values

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




# Python3 code to demonstrate
# Type conversion in list of dicts.
# using naive method
 
# initializing list of dictionary
test_list = [{'a' : '1', 'b' : '2'}, { 'c' : '3', 'd' : '4'}]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using naive method
# type conversation in list of dicts.
for dicts in test_list:
    for keys in dicts:
        dicts[keys] = int(dicts[keys])
     
# printing result
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




# Python3 code to demonstrate
# Type conversion in list of dicts.
# using items() + list comprehension
 
# initializing list of dictionary
test_list = [{'a' : '1', 'b' : '2'}, { 'c' : '3', 'd' : '4'}]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using items() + list comprehension
# type conversation in list of dicts.
res = [dict([key, int(value)]
       for key, value in dicts.items())
       for dicts in test_list]
     
# printing result
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




# create a list of dictionaries
test_list = [{'a' : '1', 'b' : '2'}, { 'c' : '3', 'd' : '4'}]
 
# use the map() function to apply the lambda function to each dictionary in the list
# the lambda function creates a new dictionary with the keys and values from the original dictionary
# but the values are converted to integers using int()
result = [{k: int(v) for k, v in d.items()} for d in map(lambda x: dict(x), test_list)]
 
# print the resulting list of dictionaries
print(result)
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

[{'a': 1, 'b': 2}, {'c': 3, 'd': 4}]
RELATED ARTICLES

Most Popular

Recent Comments