Python – Dictionary Key’s Product in list

• Last Updated : 18 Mar, 2023

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

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

Method #3:  Using a list comprehension + reduce() from the functools module

Step by step Algorithm:

1. Import the reduce function from the functools module.
2. Define a list of dictionaries and the key whose product we want to find.
3. Use a list comprehension to extract the values of the key from each dictionary in the list.
4. Use the reduce function to multiply all the values together and assign the result to the variable product.
5. 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
```

Complexity Analysis:

Time complexity: O(n), where n is the number of dictionaries in the list.

This is because we need to loop through each dictionary in the list once to extract the values of the key and then we use the reduce function to multiply all the values together.

Space complexity: O(n), where n is the number of dictionaries in the list.

This is because we create a new list to store the values of the key for each dictionary in the list. The space complexity could be reduced by using a generator expression instead of a list comprehension to extract the values, which would reduce the memory usage to O(1).

My Personal Notes arrow_drop_up