Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Dictionary Key’s Product in list

  • Last Updated : 23 Jan, 2020

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 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# 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

 

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!