Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Multiplication across Like Keys Value list elements

Improve Article
Save Article
  • Last Updated : 10 Mar, 2023
Improve Article
Save Article

Given two dictionaries with value lists, perform element wise like keys multiplication.

Input : test_dict1 = {“Gfg” : [4, 6], “Best” : [8, 6], “is” : [9, 3]}, test_dict2 = {“Gfg”: [8, 4], “Best” : [6, 3], “is” : [9, 8]} Output : {‘Gfg’: [32, 24], ‘Best’: [48, 18], ‘is’: [81, 24]} Explanation : 4 * 8 = 32, 6 * 4 = 24 and so on, hence new list value. Input : test_dict1 = {“Gfg” : [4, 6], “Best” : [8, 6]}, test_dict2 = {“Gfg”: [8, 4], “Best” : [6, 3]} Output : {‘Gfg’: [32, 24], ‘Best’: [48, 18]} Explanation : 4 * 8 = 32, 6 * 4 = 24 and so on, hence new list value.

Method : Using dictionary comprehension + zip()

This is one of the ways in which this task can be performed. In this, we perform the task of combining keys using zip() and use zip() again for combining like values. The dictionary comprehension is used to perform construction of new list.

Python3




# Python3 code to demonstrate working of
# Multiplication across Like Keys Value list elements
# Using dictionary comprehension + zip()
 
# initializing dictionaries
test_dict1 = {"Gfg" : [4, 6, 7], "Best" : [8, 6, 4], "is" : [9, 3, 4]}
test_dict2 = {"Gfg": [8, 4, 3], "Best" : [6, 3, 1], "is" : [9, 8, 2]}
 
# printing original lists
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
# Using zip() to perform link keys and values
res = {key: [ele1 * ele2 for (ele1, ele2) in zip(test_dict1[key], val2)]
       for (key, val2) in zip(test_dict1.keys(), test_dict2.values())}
 
# printing result
print("The constructed dictionary : " + str(res))

Output

The original dictionary 1 is : {'Gfg': [4, 6, 7], 'Best': [8, 6, 4], 'is': [9, 3, 4]}
The original dictionary 2 is : {'Gfg': [8, 4, 3], 'Best': [6, 3, 1], 'is': [9, 8, 2]}
The constructed dictionary : {'Gfg': [32, 24, 21], 'Best': [48, 18, 4], 'is': [81, 24, 8]}

Time Complexity: O(n), where n is the elements of dictionary
Auxiliary Space: O(n), where n is the size of dictionary

Method 2: Use a for loop to iterate through the keys and values of the dictionaries:

Python




# Python3 code to demonstrate working of
# Multiplication across Like Keys Value list elements
# Using for loop
 
# initializing dictionaries
test_dict1 = {"Gfg" : [4, 6, 7], "Best" : [8, 6, 4], "is" : [9, 3, 4]}
test_dict2 = {"Gfg": [8, 4, 3], "Best" : [6, 3, 1], "is" : [9, 8, 2]}
 
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
 
# Multiplying the values of the like keys
res = {}
for key in test_dict1:
    if key in test_dict2:
        res[key] = [test_dict1[key][i] * test_dict2[key][i] for i in range(len(test_dict1[key]))]
 
# printing result
print("The constructed dictionary : " + str(res))

Output

The original dictionary 1 is : {'is': [9, 3, 4], 'Gfg': [4, 6, 7], 'Best': [8, 6, 4]}
The original dictionary 2 is : {'is': [9, 8, 2], 'Gfg': [8, 4, 3], 'Best': [6, 3, 1]}
The constructed dictionary : {'is': [81, 24, 8], 'Gfg': [32, 24, 21], 'Best': [48, 18, 4]}

Time complexity: O(nm), where n is the number of keys in the dictionaries and m is the length of the value lists. 
Auxiliary space: O(nm) to store the result dictionary.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!