Python - Dictionary Values Division
Last Updated :
27 Apr, 2023
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
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Dictionary Values Mean
Given a dictionary, find the mean of all the values present. Input : test_dict = {"Gfg" : 4, "is" : 4, "Best" : 4, "for" : 4, "Geeks" : 4} Output : 4.0 Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean. Input : test_dict = {"Gfg" : 5, "is" : 10, "Best" : 15} Output : 10.0 Explanation : Mean of
4 min read
Inverse Dictionary Values List - Python
We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]}Using defaul
2 min read
Python - Print dictionary of list values
In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python. Example: {'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value}
4 min read
Python - Value length dictionary
Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor
4 min read
Set from Dictionary Values - Python
The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set.For example, given a dictionary like d = {'A': 4, '
3 min read
Python Print Dictionary Keys and Values
When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values.Example: Using print() MethodPythonmy_dict = {'a': 1, 'b': 2, 'c': 3} print("Keys:", l
2 min read
Even Values Update in Dictionary - Python
The task of updating even values in a dictionary in Python involves modifying the values associated with specific keys based on a condition, typically checking whether the values are even. For example, consider a dictionary like d = {'gfg': 6, 'is': 4, 'best': 7}. The goal is to update the values by
3 min read
Get Index of Values in Python Dictionary
Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read
Python | Initialize dictionary with None values
Sometimes, while working with dictionaries, we might have a utility in which we need to initialize a dictionary with None values so that they can be altered later. This kind of application can occur in cases of memoization in general or competitive programming. Let's discuss certain ways in which th
4 min read
Get List of Values From Dictionary - Python
We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t
2 min read