Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Tuple value product in dictionary

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




# Python3 code to demonstrate working of
# Dictionary Tuple Values Product
# Using tuple() + loop + zip() + values()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Dictionary Tuple Values Product
# Using tuple() + loop + zip() + values()
res = tuple(prod(x) for x in zip(*test_dict.values()))
 
# printing result
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




# Python3 code to demonstrate working of
# Dictionary Tuple Values Product
# Using tuple() + map() + values()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Dictionary Tuple Values Product
# Using tuple() + map() + values()
temp = []
for sub in test_dict.values():
    temp.append(list(sub))
res = tuple(map(prod, temp))
 
# printing result
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




# Python3 code to demonstrate working of
# Dictionary Tuple Values Product
# Using reduce() + lambda + values()
   
# importing functools for reduce()
import functools
   
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
   
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
   
# Dictionary Tuple Values Product
# Using reduce() + lambda + values()
res = tuple(functools.reduce(lambda x, y : x * y, sub) for sub in zip(*test_dict.values()))
   
# printing result
print("The product from each index is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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()

  1. The numpy module is imported as np.
  2. A dictionary test_dict is initialized with three key-value pairs. Each value is a tuple of three integers.
  3. 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.
  4. A list comprehension is used to extract each value from the dictionary, then a nested list is created from these values.
  5. 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.
  6. 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
 
# Initializing dictionary
test_dict = {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
 
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Dictionary Tuple Values Product
# Using list comprehension and numpy.prod()
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]])))
 
# Printing result
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments