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 product 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 Keys Product # Using dictionary comprehension + keys() # Initialize dictionaries test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 } 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 Keys Product res = {key: test_dict2[key] * test_dict1.get(key, 0 ) for key in test_dict2.keys()} # printing result print ("The product dictionary is : " + str (res)) |
The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6} The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10} The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
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 minus operator can perform the task of multiplication.
Python3
# Python3 code to demonstrate working of # Dictionary Keys Product # Using Counter() + "*" operator from collections import Counter # Initialize dictionaries test_dict1 = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 } 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 Keys Product temp1 = Counter(test_dict1) temp2 = Counter(test_dict2) res = Counter({key : temp1[key] * temp2[key] for key in temp1}) # printing result print ("The product dictionary is : " + str ( dict (res))) |
The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6} The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10} The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
Method #3 : Using dictionary+ zip()
This method uses the zip function to pair up the keys from dict1 with the corresponding values calculated as the product of the values from both dictionaries. The resulting pairs are passed to the dictionary constructor, which creates the product_dict.
Python3
# Define the original dictionaries dict1 = { 'best' : 7 , 'is' : 4 , 'gfg' : 6 } dict2 = { 'best' : 10 , 'is' : 6 , 'gfg' : 10 } # Use zip and a dictionary constructor to create the product dictionary product_dict = dict ( zip (dict1, (dict1[key] * dict2[key] for key in dict1))) # Print the resulting product dictionary print ( "The product dictionary is:" , product_dict) |
The product dictionary is: {'best': 70, 'is': 24, 'gfg': 60}
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4:Using for loop
Algorithm:
- Initialize an empty dictionary called product_dict
- For each key in dict1, do the following:
a. If the key is not in dict2, skip to the next key
b. Multiply the value in dict1 with the value in dict2 for the corresponding key
c. Add a new key-value pair to the product_dict with the key from dict1 and the product from step b - Return the product_dict
Python3
#Define the original dictionaries dict1 = { 'best' : 7 , 'is' : 4 , 'gfg' : 6 } dict2 = { 'best' : 10 , 'is' : 6 , 'gfg' : 10 } #Use for loop to create the product dictionary product_dict = {} for key in dict1: product_dict[key] = dict1[key] * dict2[key] #Print the resulting product dictionary print ( "The product dictionary is:" , product_dict) |
The product dictionary is: {'best': 70, 'is': 24, 'gfg': 60}
The time complexity of both implementations is O(n), where n is the number of keys in the dictionaries. This is because both methods loop through the keys of the dictionaries once.
The space complexity of the first implementation using zip and a dictionary constructor is O(n), as it creates a new list object of size n and a new dictionary object of size n.
METHOD 5:Using the map() function
APPROACH:
This program takes two dictionaries as input and calculates the product of values of the common keys between them. The output is a new dictionary containing these products.
ALGORITHM:
1.Define two dictionaries dict1 and dict2.
2.Define a lambda function multiply to calculate the product of two values.
3.Initialize an empty dictionary product_dict to store the products of the values of the common keys between dict1 and dict2.
4.Iterate over the keys of dict1.
5.If a key is present in dict2, calculate the product of the values of the corresponding keys and add it to product_dict.
6.Print the product_dict
Python3
# Sample input dict1 = { 'best' : 7 , 'is' : 4 , 'gfg' : 6 } dict2 = { 'best' : 10 , 'is' : 6 , 'gfg' : 10 } # Define a function to calculate the product of two values def multiply(x, y): return x * y # Initialize the product dictionary product_dict = {} # Iterate over the keys of the first dictionary for key in dict1.keys(): # If the key is present in the second dictionary if key in dict2: # Calculate the product of the values of the corresponding keys product_dict[key] = multiply(dict1[key], dict2[key]) # Output print (f "The product dictionary is : {product_dict}" ) |
The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
Time Complexity:
The time complexity of this program is O(n), where n is the number of keys in dict1.
Space Complexity:
The space complexity of this program is O(n), where n is the number of common keys between dict1 and dict2. This is because we are storing the products of the values of these keys in the product_dict.