Open In App

Python – Dictionary value lists lengths product

Given a dictionary with values as lists, compute the lengths of each list, and find product of all lengths.

Input : test_dict = {‘Gfg’ : [6, 5, 9, 3], ‘is’ : [1, 3, 4], ‘best’ :[9, 16]} 
Output : 24 
Explanation : 4 * 3 * 2 = 24. Length of lists are 4, 3, and 2. 



Input : test_dict = {‘Gfg’ : [6, 5, 3], ‘is’ : [1, 3, 4], ‘best’ :[9, 16]} 
Output : 18 
Explanation : 3 * 3 * 2 = 18. Length of lists are 3, 3, and 2.

Method #1 : Using loop + len()

This is one of the ways in which this task can be performed. In this, we iterate for all the values and use len() to get length of all value lists, post which we perform the multiplication of whole data.






# Python3 code to demonstrate working of
# Dictionary value lists lengths product
# Using loop + len()
 
# initializing dictionary
test_dict = {'Gfg' : [6, 5, 9, 3, 10],
             'is' : [1, 3, 4],
             'best' :[9, 16]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using loop to iterate through all keys
res = 1
for key in test_dict:
     
    # len() used to get length of each value list
    res = res * len(test_dict[key])   
 
# printing result
print("The computed product : " + str(res))

Output
The original dictionary is : {'Gfg': [6, 5, 9, 3, 10], 'is': [1, 3, 4], 'best': [9, 16]}
The computed product : 30

Time complexity: O(n), where n is the total number of values in all the lists of the dictionary.
Auxiliary space: O(1), because the only extra space used is for the variable res which stores the product of the lengths of all value lists, and it is a constant amount of space regardless of the size of the input. The original dictionary is not modified during the execution of the program.

Method #2 : Using map() + lambda + reduce() 

The combination of above functions provide one-liner approach to solve this problem. In this, we use map() to get lengths of all lists extending len() to each list, lambda is used to get product and reduce to combine.




# Python3 code to demonstrate working of
# Dictionary value lists lengths product
# Using map() + lambda + reduce()
from functools import reduce
 
# initializing dictionary
test_dict = {'Gfg' : [6, 5, 9, 3, 10],
             'is' : [1, 3, 4],
             'best' :[9, 16]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# values() used to get all lists of keys
res = reduce(lambda sub1, sub2: sub1 * sub2, map(len, test_dict.values()))
 
# printing result
print("The computed product : " + str(res))

Output
The original dictionary is : {'Gfg': [6, 5, 9, 3, 10], 'is': [1, 3, 4], 'best': [9, 16]}
The computed product : 30

Time complexity: O(N), where N is the total number of elements in all the lists in the dictionary.
Auxiliary space: O(1), since only a single integer variable is used to store the product of lengths of all the lists in the dictionary.

Method #3: Using list comprehension and reduce()

Step-by-step approach:

Below is the implementation of the above approach:




from functools import reduce
 
# initializing dictionary
test_dict = {'Gfg' : [6, 5, 9, 3, 10],
             'is' : [1, 3, 4],
             'best' :[9, 16]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using list comprehension to get length of each value list
lengths = [len(val) for val in test_dict.values()]
 
# using reduce() to multiply all the lengths
res = reduce(lambda x, y: x*y, lengths)
 
# printing result
print("The computed product : " + str(res))

Output
The original dictionary is : {'Gfg': [6, 5, 9, 3, 10], 'is': [1, 3, 4], 'best': [9, 16]}
The computed product : 30

Time complexity: O(n), where n is the total number of elements in all the lists in the dictionary.
Auxiliary space: O(1), as only one variable is used to store the intermediate and final results.


Article Tags :