Python – Dictionary Key’s Product in list

Many operations such as grouping and conversions are possible using Python dictionaries. But sometimes, we can also have a problem in which we need to perform the product of values of key in the dictionary list. This task is common in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + list comprehension This is the one-liner approach to perform the task of getting the product of particular key while iterating to the similar keys in list of dictionaries using list comprehension.
Python3
# Python3 code to demonstrate working of# Dictionary Key's Product in list# Using loop + list comprehensiondef prod(val) : res = 1 for ele in val: res *= ele return res# Initialize listtest_list = [{'gfg' : 1, 'is' : 2, 'best' : 3}, {'gfg' : 7, 'is' : 3, 'best' : 5}, {'gfg' : 9, 'is' : 8, 'best' : 6}] # printing original listprint("The original list is : " + str(test_list))# Dictionary Key's Product in list# Using loop + list comprehensionres = prod(sub['gfg'] for sub in test_list)# printing resultprint("The product of particular key is : " + str(res)) |
The original list is : [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]
The product of particular key is : 63
Time Complexity: O(n*n), where n is the length of the dictionary
Auxiliary Space: O(1) constant additional space is required
Method #2 : Using loop + itemgetter() + map() The combination of these functions can also be used to perform this task. In this, the main difference is that the comprehension task is done by map() and the key access task is done by the itemgetter().
Python3
# Python3 code to demonstrate working of# Dictionary Key's Product in list# Using loop + itemgetter() + map()import operatordef prod(val) : res = 1 for ele in val: res *= ele return res# Initialize listtest_list = [{'gfg' : 1, 'is' : 2, 'best' : 3}, {'gfg' : 7, 'is' : 3, 'best' : 5}, {'gfg' : 9, 'is' : 8, 'best' : 6}] # printing original listprint("The original list is : " + str(test_list))# Dictionary Key's Product in list# Using loop + itemgetter() + map()res = prod(map(operator.itemgetter('gfg'), test_list))# printing resultprint("The product of particular key is : " + str(res)) |
The original list is : [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]
The product of particular key is : 63
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using a list comprehension + reduce() from the functools module
Step by step Algorithm:
- Import the reduce function from the functools module.
- Define a list of dictionaries and the key whose product we want to find.
- Use a list comprehension to extract the values of the key from each dictionary in the list.
- Use the reduce function to multiply all the values together and assign the result to the variable product.
- Print the original list of dictionaries and the product of the particular key.
Python3
# Import the reduce function from the functools modulefrom functools import reduce# Define the list of dictionarieslist_of_dicts = [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]# Define the key whose product we want to findkey = 'gfg'# Use a list comprehension to extract the values of the key from each dictionaryvalues = [d[key] for d in list_of_dicts]# Use the reduce function to multiply all the values togetherproduct = reduce(lambda x, y: x * y, values)# Print the resultprint("The original list is :", list_of_dicts)print("The product of particular key is :", product) |
The original list is : [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]
The product of particular key is : 63
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #4: Using the math library to perform multiplication of keys:
- Import the math library.
- Define a list of dictionaries list_of_dicts
- Define the key whose product you want to find. In this case, we want to find the product of the values associated with the key ‘gfg’.
- Use a list comprehension to extract the values associated with the key ‘gfg’ from each dictionary in list_of_dicts.
- Use the math.prod() function to multiply all the values together.
- Print the original list and the product of the key
Python3
#Import the math libraryimport math#Define the list of dictionarieslist_of_dicts = [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]#Define the key whose product we want to findkey = 'gfg'#Use a list comprehension to extract the values of the key from each dictionaryvalues = [d[key] for d in list_of_dicts]#Use the math library to multiply all the values togetherproduct = math.prod(values)#Print the resultprint("The original list is :", list_of_dicts)print("The product of particular key is :", product) |
Output
The original list is : [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]
The product of particular key is : 63
Time complexity: O(n), where n is the length of the list of dictionaries.
Auxiliary space: O(1), which is constant space.
Method #7: Using numpy product() function
Step-by-step approach:
- Import the numpy module.
- Define the list of dictionaries.
- Define the key whose product we want to find.
- Use a list comprehension to extract the value of the key from each dictionary in the list.
- Use the numpy.product() function to calculate the product of the values.
- Print the result.
Python3
# Import the numpy moduleimport numpy as np# Define the list of dictionarieslist_of_dicts = [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]# Define the key whose product we want to findkey = 'gfg'# Use a list comprehension to extract the value of the key from each dictionary in the listvalues = [d[key] for d in list_of_dicts]# Use the numpy product() function to calculate the product of the valuesproduct = np.product(values)# Print the resultprint("The original list is:", list_of_dicts)print("The product of the particular key is:", product) |
Output
The original list is: [{'is': 2, 'best': 3, 'gfg': 1}, {'is': 3, 'best': 5, 'gfg': 7}, {'is': 8, 'best': 6, 'gfg': 9}]
The product of the particular key is: 63
Time complexity: O(n), where n is the number of dictionaries in the list.
Auxiliary space: O(n), because we create a list of values extracted from the list of dictionaries.



