Python – Value length dictionary
Last Updated :
22 Apr, 2023
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
test_dict = { 1 : 'gfg' , 2 : 'is' , 3 : 'best' }
print ( "The original dictionary is : " + str (test_dict))
res = {}
for val in test_dict.values():
res[val] = len (val)
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
test_dict = { 1 : 'gfg' , 2 : 'is' , 3 : 'best' }
print ( "The original dictionary is : " + str (test_dict))
res = {val: len (val) for val in test_dict.values()}
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
test_dict = { 1 : 'gfg' , 2 : 'is' , 3 : 'best' }
print ( "The original dictionary is : " + str (test_dict))
res = dict ( map ( lambda x: (x[ 1 ], len (x[ 1 ])), test_dict.items()))
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
test_dict = { 1 : 'gfg' , 2 : 'is' , 3 : 'best' }
print ( "The original dictionary is : " + str (test_dict))
res = defaultdict( int )
for val in test_dict.values():
res[val] = len (val)
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
Please Login to comment...