Open In App

Python – Value length dictionary

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

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 performed. 

Method #1 : Using loop + len() 

This is one of the ways in which this task can be performed. In this, we extract the value of the dictionary and map it with its length computed using len() method. 

Python3




# Python3 code to demonstrate working of
# Value length dictionary
# Using loop + len()
 
# initializing dictionary
test_dict = {1: 'gfg', 2: 'is', 3: 'best'}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Value length dictionary
# Using loop + len()
res = {}
for val in test_dict.values():
    res[val] = len(val)
 
# printing result
print("The value-size mapped dictionary is : " + str(res))


Output : 

The original dictionary is : {1: 'gfg', 2: 'is', 3: 'best'}
The value-size mapped dictionary is : {'is': 2, 'best': 4, 'gfg': 3}

Method #2: Using dictionary comprehension

This task can also be performed using dictionary comprehension. In this, we perform tasks similar to the above method, just in a smaller form. 

Python3




# Python3 code to demonstrate working of
# Value length dictionary
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {1: 'gfg', 2: 'is', 3: 'best'}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Value length dictionary
# Using dictionary comprehension
res = {val: len(val) for val in test_dict.values()}
 
# printing result
print("The value-size mapped dictionary is : " + str(res))


Output : 

The original dictionary is : {1: 'gfg', 2: 'is', 3: 'best'}
The value-size mapped dictionary is : {'is': 2, 'best': 4, 'gfg': 3}

Method #3: Here is another approach using map and len functions:

Python3




# Python3 code to demonstrate working of
# Value length dictionary
# Using map() and len()
       
# initializing dictionary
test_dict = {1 : 'gfg', 2 : 'is', 3 : 'best'}
   
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
   
# Value length dictionary
# Using map() and len()
res = dict(map(lambda x: (x[1], len(x[1])), test_dict.items()))
   
# printing result
print("The value-size mapped dictionary is : " + str(res))


Output

The original dictionary is : {1: 'gfg', 2: 'is', 3: 'best'}
The value-size mapped dictionary is : {'gfg': 3, 'is': 2, 'best': 4}

Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary Space: O(n), as we create a new dictionary to store the value-length mapping.

Method #7: Using collections.defaultdict() and loop

This approach uses the defaultdict() function from the collections module to create a dictionary with default values of 0. Then, a loop is used to iterate over the values of the original dictionary and update the defaultdict with the length of each value.

Steps:

Import the defaultdict function from the collections module
Define the original dictionary and print it
Create a defaultdict with default values of 0
Iterate over the values of the original dictionary and update the defaultdict with the length of each value
Print the new defaultdict

Python3




from collections import defaultdict
 
# initializing dictionary
test_dict = {1: 'gfg', 2: 'is', 3: 'best'}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Value length dictionary using defaultdict and loop
res = defaultdict(int)
for val in test_dict.values():
    res[val] = len(val)
 
# printing result
print("The value-size mapped dictionary is : " + str(dict(res)))


Output

The original dictionary is : {1: 'gfg', 2: 'is', 3: 'best'}
The value-size mapped dictionary is : {'gfg': 3, 'is': 2, 'best': 4}

Time complexity: O(n), where n is the number of items in the dictionary
Auxiliary space: O(n), where n is the number of items in the dictionary



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads