Open In App

Calculating the Product of List Lengths in a Dictionary - Python

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of calculating the product of the lengths of lists in a dictionary involves iterating over the dictionary’s values, which are lists and determining the length of each list. These lengths are then multiplied together to get a single result. For example, if d = {'A': [1, 2, 3], 'B': [4, 5], 'C': [6, 7, 8, 9]}, the product of the lengths of the lists is calculated as 3 * 2 * 4 = 24.

Using math.prod()

math.prod() is a highly optimized function that calculates the product of an iterable efficiently. By using a generator expression to iterate over the dictionary values, we avoid unnecessary memory consumption and achieve an optimal solution.

Python
import math

d = {'Gfg': [6, 5, 9, 3, 10],'is': [1, 3, 4],'best': [9, 16]}

res = math.prod(len(lst) for lst in d.values())
print(res)

Output
30

Explanation: for loop iterates over the dictionary's list values, calculates their lengths with len(lst), and math.prod() efficiently multiplies these lengths using a generator expression, avoiding intermediate lists.

Using functools.reduce()

reduce() from functools applies an operation cumulatively, making it an efficient way to compute the product without explicit loops. By combining it with operator.mul, we achieve a clean, functional approach to multiplying the lengths of dictionary value lists.

Python
from functools import reduce
import operator

d = {'Gfg': [6, 5, 9, 3, 10],'is': [1, 3, 4],'best': [9, 16]}

res = reduce(operator.mul, map(len, d.values()), 1)
print(res)

Output
30

Explanation: reduce() applies map(len, d.values()) to calculate their lengths and operator.mul multiplies these lengths together, starting with an initial value of 1.

Using list comprehension

List comprehension generate a list of lengths from dictionary values. While it introduces an intermediate list increasing memory usage , it remains a simple and readable method for computing the product when combined with math.prod() or reduce().

Python
import math

d = {'Gfg': [6, 5, 9, 3, 10], 'is': [1, 3, 4], 'best': [9, 16]}

lengths = [len(lst) for lst in d.values()]
res = math.prod(lengths)
print(res)

Output
30

Explanation: list comprehension iterates over the dictionary's list values, calculates their lengths with len(lst) and stores these lengths in the lengths list. The math.prod() then computes the product of the lengths .

Using for loop

For loop provides a straightforward and beginner-friendly approach to multiplying the lengths of dictionary values. Though it is not as concise as math.prod() or reduce(), it is still efficient and does not introduce extra memory overhead like list comprehension.

Python
import math

d = {'Gfg': [6, 5, 9, 3, 10],'is': [1, 3, 4],'best': [9, 16]}

res = 1
for lst in d.values():
    res *= len(lst)

print(res)

Output
30

Explanation: for loop iterates over the dictionary's list values, calculates their lengths using len(lst) and multiplies these lengths together, updating the res variable.


Next Article

Similar Reads