Open In App

Python – Dictionary Values Division

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Let’s discuss division of like key values and ways to solve it in this article. 

Method #1 : Using dictionary comprehension + keys() The combination of above two can be used to perform this particular task. This is just a shorthand to the longer method of loops and can be used to perform this task in one line. 

Python3




# Python3 code to demonstrate working of
# Dictionary Values Division
# Using dictionary comprehension + keys()
 
# Initialize dictionaries
test_dict1 = {'gfg' : 20, 'is' : 24, 'best' : 100}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
 
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
 
# Using dictionary comprehension + keys()
# Dictionary Values Division
res = {key: test_dict1[key] // test_dict2.get(key, 0)
                        for key in test_dict1.keys()}
 
# printing result
print("The divided dictionary is : " + str(res))


Output : 

The original dictionary 1 : {'is': 24, 'best': 100, 'gfg': 20}
The original dictionary 2 : {'is': 6, 'best': 10, 'gfg': 10}
The divided dictionary is : {'is': 4, 'best': 10, 'gfg': 2}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.

Auxiliary space complexity: O(n), where n is the number of key-value pairs in the dictionary.

  Method #2 : Using Counter() + “//” operator The combination of above method can be used to perform this particular task. In this, the Counter function converts the dictionary in the form in which the divide operator can perform the task of division. 

Python3




# Python3 code to demonstrate working of
# Dictionary Values Division
# Using Counter() + "//" operator
from collections import Counter
 
# Initialize dictionaries
test_dict1 = {'gfg' : 20, 'is' : 24, 'best' : 100}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
 
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
 
# Using Counter() + "//" operator
# Dictionary Values Division
temp1 = Counter(test_dict1)
temp2 = Counter(test_dict2)
res = Counter({key : temp1[key] // temp2[key] for key in temp1})
 
# printing result
print("The division dictionary is : " + str(dict(res)))


Output : 

The original dictionary 1 : {'is': 24, 'best': 100, 'gfg': 20}
The original dictionary 2 : {'is': 6, 'best': 10, 'gfg': 10}
The divided dictionary is : {'is': 4, 'best': 10, 'gfg': 2}

Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #3 : Using loop + dict.update()
This is the brute force approach which can also be used to perform this task. In this, we perform the direct division of like keys and store the result in the result dictionary.

Python3




# Python3 code to demonstrate working of
# Dictionary Values Division
# Using loop + dict.update()
   
# Initialize dictionaries
test_dict1 = {'gfg' : 20, 'is' : 24, 'best' : 100}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
   
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
   
# Using loop + dict.update()
# Dictionary Values Division
res = {}
for key in test_dict1:
    res[key] = test_dict1[key] // test_dict2.get(key, 0)
   
# printing result
print("The division dictionary is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary 1 : {'gfg': 20, 'is': 24, 'best': 100}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The division dictionary is : {'gfg': 2, 'is': 4, 'best': 10}

Time complexity : O(n) 
Space complexity : O(n) 

Method #5: Using zip() and map() functions

This method uses zip() function to combine the keys of both dictionaries into a single iterable, and map() function to apply a lambda function to each pair of corresponding values from both dictionaries. The lambda function performs integer division of the first value by the second value if the second value is non-zero, and returns 0 otherwise. The resulting iterable is converted back to a dictionary using the dict() constructor, with the keys from the first dictionary and the values obtained from the lambda function.

Python3




# Python3 code to demonstrate working of
# Dictionary Values Division
# Using zip() and map() functions
 
# Initialize dictionaries
test_dict1 = {'gfg' : 20, 'is' : 24, 'best' : 100}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
 
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
 
# Using zip() and map() functions
# Dictionary Values Division
res = dict(zip(test_dict1.keys(), map(lambda x, y: x // y if y else 0, test_dict1.values(), test_dict2.values())))
 
# printing result
print("The division dictionary is : " + str(res))


Output

The original dictionary 1 : {'gfg': 20, 'is': 24, 'best': 100}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The division dictionary is : {'gfg': 2, 'is': 4, 'best': 10}

The time complexity of this code is O(n), where n is the number of key-value pairs in the dictionaries.

The auxiliary space of this code is O(n), because we create a new dictionary res with n key-value pairs that we populate using the dict() constructor.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads