Open In App

Python – Dictionary Key’s Product in list

Improve
Improve
Like Article
Like
Save
Share
Report

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 comprehension
 
def prod(val) :    
    res = 1        
    for ele in val:        
        res *= ele        
    return res
 
# Initialize list
test_list = [{'gfg' : 1, 'is' : 2, 'best' : 3}, {'gfg' : 7, 'is' : 3, 'best' : 5}, {'gfg' : 9, 'is' : 8, 'best' : 6}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Dictionary Key's Product in list
# Using loop + list comprehension
res = prod(sub['gfg'] for sub in test_list)
 
# printing result
print("The product of particular key is : " + str(res))


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*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 operator
 
def prod(val) :    
    res = 1        
    for ele in val:        
        res *= ele        
    return res
 
# Initialize list
test_list = [{'gfg' : 1, 'is' : 2, 'best' : 3}, {'gfg' : 7, 'is' : 3, 'best' : 5}, {'gfg' : 9, 'is' : 8, 'best' : 6}]
 
# printing original list
print("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 result
print("The product of particular key is : " + str(res))


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*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 module
from functools import reduce
 
# Define the list of dictionaries
list_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 find
key = 'gfg'
 
# Use a list comprehension to extract the values of the key from each dictionary
values = [d[key] for d in list_of_dicts]
 
# Use the reduce function to multiply all the values together
product = reduce(lambda x, y: x * y, values)
 
# Print the result
print("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 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 library
import math
 
#Define the list of dictionaries
list_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 find
key = 'gfg'
 
#Use a list comprehension to extract the values of the key from each dictionary
values = [d[key] for d in list_of_dicts]
 
#Use the math library to multiply all the values together
product = math.prod(values)
 
#Print the result
print("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 module
import numpy as np
 
# Define the list of dictionaries
list_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 find
key = 'gfg'
 
# Use a list comprehension to extract the value of the key from each dictionary in the list
values = [d[key] for d in list_of_dicts]
 
# Use the numpy product() function to calculate the product of the values
product = np.product(values)
 
# Print the result
print("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.



Last Updated : 16 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads