Calculating the Product of List Lengths in a Dictionary - Python
Last Updated : 10 Feb, 2025
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)
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.
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)
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)
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)
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.
Similar Reads
Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
5 min read
How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa
3 min read
Python - Add Dictionary Items In Python, dictionaries are a built-in data structure that stores key-value pairs. Adding items to a dictionary is a common operation when you're working with dynamic data or building complex data structures. This article covers various methods for adding items to a dictionary in Python.Adding Items
3 min read
Python Add Dictionary Key In Python, dictionaries are unordered collections of key-value pairs. Each item in a dictionary is accessed by its unique key, which allows for efficient storage and retrieval of data. While dictionaries are commonly used with existing keys, adding a new key is an essential operation when working wi
4 min read
Python Access Dictionary In Python, dictionaries are powerful and flexible data structures used to store collections of data in key-value pairs. To get or "access" a value stored in a dictionary, we need to know the corresponding key. In this article, we will explore all the ways to access dictionary values, keys, and both
4 min read
Python Change Dictionary Item A common task when working with dictionaries is updating or changing the values associated with specific keys. This article will explore various ways to change dictionary items in Python.1. Changing a Dictionary Value Using KeyIn Python, dictionaries allow us to modify the value of an existing key.
3 min read
Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop
2 min read
Get length of dictionary in Python Python provides multiple methods to get the length, and we can apply these methods to both simple and nested dictionaries. Letâs explore the various methods.Using Len() FunctionTo calculate the length of a dictionary, we can use Python built-in len() method. It method returns the number of keys in d
3 min read
Python - Value length dictionary Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor
4 min read
Python - Dictionary values String Length Summation Sometimes, while working with Python dictionaries we can have problem in which we need to perform the summation of all the string lengths which as present as dictionary values. This can have application in many domains such as web development and day-day programming. Lets discuss certain ways in whi
4 min read