Open In App

Python – Dictionary value lists lengths product

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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:

  • 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
 
# 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.



Previous Article
Next Article

Similar Reads

Python - Cumulative product of dictionary value lists
Sometimes, while working with Python dictionaries, we can have it’s values as lists. In this can we can have a problem that we just require the product of elements in those list as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let’s discuss certain ways in which this task can be performedMetho
4 min read
Python program to sort Dictionary by Key Lengths
Given Dictionary, sort by its key lengths. Input : test_dict = {"Gfg" : 4, "is" : 1, "best" : 0, "for" : 3, "geeks" : 3} Output : {'is': 1, 'Gfg': 4, 'for': 3, 'best': 0, 'geeks': 3} Explanation : 2 < 3 = 3 < 4 < 5, are sorted lengths in order. Input : test_dict = {"Gfg" : 4, "for" : 3, "geeks" : 3} Output : {'Gfg': 4, 'for': 3, 'geeks': 3
4 min read
Python - Value list lengths
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the length of list as tuple attribute. Let’s discuss certain ways in which this task can be performed. Method
5 min read
Python - Convert Lists into Similar key value lists
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corresponding list, are mapped to 6. Input : test_list1
12 min read
Merge Key Value Lists into Dictionary Python
Sometimes, while working with lists, we can come forward with a problem in which we need to perform the merge function in which we have the key list and need to create dictionary mapping keys with corresponding value in other list. Let's discuss certain ways in which this task can be performed. Merge Key Value Lists into Dictionary Python Using zip
8 min read
Python | Convert list of tuples to dictionary value lists
One among the problem of interconversion of data types in python is conversion of list of tuples to dictionaries, in which the keys are 1st elements of tuple, which are uniquely identified as keys in dictionary and it's corresponding value as a list of the corresponding value of respective keys as tuple's second element. Let's discuss how to solve
9 min read
Python | Iterate through value lists dictionary
While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List comprehension can be used to perform this particula
4 min read
Python | Concatenate dictionary value lists
Sometimes, while working with dictionaries, we might have a problem in which we have lists as it's value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using sum() + values() This is the most recommended m
5 min read
Python - Convert Key-Value list Dictionary to List of Lists
Sometimes, while working with a Python dictionary, we can have a problem in which we need to perform the flattening of a key-value pair of dictionaries to a list and convert it to a list of lists. This can have applications in domains in which we have data. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + item
8 min read
Python - Filter odd elements from value lists in dictionary
Sometimes, while working with Python dictionaries we can have problem in which we need to perform the removal of odd elements from values list of dictionaries. This can have application in many domains including web development. Lets discuss certain ways in which this task can be performed. Method #1: Using list comprehension + dictionary comprehen
6 min read
three90RightbarBannerImg