Python – Tuple value product in dictionary
Sometimes, while working with data, we can have a problem in which we need to find the product of tuple elements that are received as values of dictionary. We may have a problem to get index wise product. Let’s discuss certain ways in which this particular problem can be solved.
Method #1 : Using tuple() + loop + zip() + values() The combination of above methods can be used to perform this particular task. In this, we just zip together equi index values extracted by values() using zip(). Then find product using respective function. Finally result is returned as index wise product as a tuple.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_dict = { 'gfg' : ( 5 , 6 , 1 ), 'is' : ( 8 , 3 , 2 ), 'best' : ( 1 , 4 , 9 )}
print ("The original dictionary is : " + str (test_dict))
res = tuple (prod(x) for x in zip ( * test_dict.values()))
print ("The product from each index is : " + str (res))
|
Output :
The original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The product from each index is : (40, 72, 18)
Method #2 : Using tuple() + map() + values() This is yet another way in which this task can be performed. The difference is that we use map() instead of loop.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_dict = { 'gfg' : ( 5 , 6 , 1 ), 'is' : ( 8 , 3 , 2 ), 'best' : ( 1 , 4 , 9 )}
print ("The original dictionary is : " + str (test_dict))
temp = []
for sub in test_dict.values():
temp.append( list (sub))
res = tuple ( map (prod, temp))
print ("The product from each index is : " + str (res))
|
Output :
The original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The product from each index is : (40, 72, 18)
## Method 3 : Using reduce() + lambda + values()
This is another way to perform the task, in which reduce() function is used to get the product.
Python3
import functools
test_dict = { 'gfg' : ( 5 , 6 , 1 ), 'is' : ( 8 , 3 , 2 ), 'best' : ( 1 , 4 , 9 )}
print ( "The original dictionary is : " + str (test_dict))
res = tuple (functools. reduce ( lambda x, y : x * y, sub) for sub in zip ( * test_dict.values()))
print ( "The product from each index is : " + str (res))
|
Output
The original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The product from each index is : (40, 72, 18)
Time complexity: O(n)
Space complexity: O(n)
Method #4: Using list comprehension and numpy.prod()
- The numpy module is imported as np.
- A dictionary test_dict is initialized with three key-value pairs. Each value is a tuple of three integers.
- The original dictionary is printed using the print() function with a string message and the str() function to convert the dictionary object to a string.
- A list comprehension is used to extract each value from the dictionary, then a nested list is created from these values.
- The numpy.prod() function is called on this nested list to calculate the product of all the elements in each position, returning a tuple of the products.
- The result tuple is printed using the print() function with a string message and the str() function to convert the tuple object to a string.
Python3
import numpy as np
test_dict = { 'gfg' : ( 5 , 6 , 1 ), 'is' : ( 8 , 3 , 2 ), 'best' : ( 1 , 4 , 9 )}
print ( "The original dictionary is : " + str (test_dict))
res = tuple (np.prod([test_dict[key][i] for key in test_dict]) for i in range ( len (test_dict[ list (test_dict.keys())[ 0 ]])))
print ( "The product from each index is : " + str (res))
|
OUTPUT :
The original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The product from each index is : (40, 72, 18)
Time complexity: O(n * m), where n is the number of keys in the dictionary and m is the length of the tuples.
Auxiliary space: O(m), where m is the length of the tuples.
Last Updated :
23 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...