Python – Dictionary value lists lengths product
Last Updated :
06 Apr, 2023
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
test_dict = { 'Gfg' : [ 6 , 5 , 9 , 3 , 10 ],
'is' : [ 1 , 3 , 4 ],
'best' :[ 9 , 16 ]}
print ( "The original dictionary is : " + str (test_dict))
res = 1
for key in test_dict:
res = res * len (test_dict[key])
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
from functools import reduce
test_dict = { 'Gfg' : [ 6 , 5 , 9 , 3 , 10 ],
'is' : [ 1 , 3 , 4 ],
'best' :[ 9 , 16 ]}
print ( "The original dictionary is : " + str (test_dict))
res = reduce ( lambda sub1, sub2: sub1 * sub2, map ( len , test_dict.values()))
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:
- Use a list comprehension to get the length of each value list in the dictionary.
- Use reduce() function to multiply all the values in the list generated in step 1.
- Print the result.
Below is the implementation of the above approach:
Python3
from functools import reduce
test_dict = { 'Gfg' : [ 6 , 5 , 9 , 3 , 10 ],
'is' : [ 1 , 3 , 4 ],
'best' :[ 9 , 16 ]}
print ( "The original dictionary is : " + str (test_dict))
lengths = [ len (val) for val in test_dict.values()]
res = reduce ( lambda x, y: x * y, lengths)
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.
Please Login to comment...