Python - Tuple value product in dictionary
Last Updated :
23 Apr, 2023
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
OutputThe 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
# 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.
Similar Reads
Python | Max/Min of tuple dictionary values
Sometimes, while working with data, we can have a problem in which we need to find the min/max of tuple elements that are received as values of dictionary. We may have a problem to get index wise min/max. Let's discuss certain ways in which this particular problem can be solved. Method #1 : Using tu
5 min read
Python - Product and Inter Summation dictionary values
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform product of entire value list and perform summation of product of each list with other. This kind of application in web development and day-day programming. Lets discuss certain ways in which this tas
4 min read
Python - Sort dictionary by Tuple Key Product
Given dictionary with tuple keys, sort dictionary items by tuple product of keys. Input : test_dict = {(2, 3) : 3, (6, 3) : 9, (8, 4): 10, (10, 4): 12} Output : {(2, 3) : 3, (6, 3) : 9, (8, 4): 10, (10, 4): 12} Explanation : 6 < 18 < 32 < 40, key products hence retains order. Input : test_d
5 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
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
Python - Dictionary Tuple Values Update
The task of updating tuple values in a dictionary involves modifying each tuple in the dictionary by applying a specific operation to its elements. In this case, the goal is to update each element of the tuple based on a given condition, such as multiplying each element by a constant. For example, g
3 min read
Python - Summation of tuple dictionary values
Sometimes, while working with data, we can have a problem in which we need to find the summation of tuple elements that are received as values of dictionary. We may have a problem to get index wise summation. Letâs discuss certain ways in which this particular problem can be solved. Method #1: Using
4 min read
Add a key value pair to Dictionary in Python
The task of adding a key-value pair to a dictionary in Python involves inserting new pairs or updating existing ones. This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key.For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'fo
3 min read
Python - Cumulative product of dictionary value lists
Sometimes, while working with Python dictionaries, we can have itâs values as lists. In this can we can have a problem that we just require the product of elements in those list as a whole. This can be a problem in Data Science in which we need to get total records in observations. Letâs discuss cer
4 min read
Python - Iterate over Tuples in Dictionary
In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. Method 1: Using index We can get the particular tuples by using an index: Syntax: dictionary_name[index] To iterate the entire tuple values in a particular index for i in range(0, len(dictionary_name[index])): print
2 min read