Python – Maximum String value length of Key

Sometimes, while working with Python dictionaries, we can have a problem in which we need to find the maximum length of all the string values of particular key. This problem can occur in domains of day-day programming and web development. Let’s discuss certain ways in which this task can be performed.

Examples –

Input:
test_list = [{‘key1’ : “abcd”, ‘key2’ : 2}, {‘key1’ : “qwertyui”, ‘key2’ : 2}, {‘key1’ : “xcvz”, ‘key3’ : 3}, {‘key1’ : None, ‘key3’ : 4}]

Output: 8
Explanation: Among all values for given key key1, qwertyui has maximum length (which is 8).

Method #1 : Using max() + len() + list comprehension
The combination of above functions can be used to solve this problem. In this, we compute the maximum string length using max() and len(). The comparison with each is bound by list comprehension.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Maximum String value length of Key
# Using max() + len() + list comprehension
  
# initializing list
test_list = [{'Gfg' "abcd", 'best' : 2}, 
             {'Gfg' "qwertyui", 'best' : 2},
             {'Gfg' "xcvz", 'good' : 3},
             {'Gfg' : None, 'good' : 4}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Key 
filt_key = 'Gfg'
  
# Maximum String value length of Key
# Using max() + len() + list comprehension
temp = (sub[filt_key] for sub in test_list)
res = max(len(ele) for ele in temp if ele is not None)
  
# printing result 
print("The maximum length key value : " + str(res)) 

chevron_right


Output :

The original list is : [{‘best’: 2, ‘Gfg’: ‘abcd’}, {‘best’: 2, ‘Gfg’: ‘qwertyui’}, {‘good’: 3, ‘Gfg’: ‘xcvz’}, {‘good’: 4, ‘Gfg’: None}]
The maximum length key value : 8

 

Method #2 : Using list comprehension + len() + max() (one liner)
The similar task can also be combined to perform in one line for compact solution.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Maximum String value length of Key
# Using max() + len() + list comprehension (one liner)
  
# initializing list
test_list = [{'Gfg' "abcd", 'best' : 2}, 
             {'Gfg' "qwertyui", 'best' : 2},
             {'Gfg' "xcvz", 'good' : 3},
             {'Gfg' : None, 'good' : 4}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Key 
filt_key = 'Gfg'
  
# Maximum String value length of Key
# Using max() + len() + list comprehension (one liner)
res = len(max(test_list, key = lambda sub: len(sub[filt_key])
                if sub[filt_key] is not None else 0)[filt_key])
  
# printing result 
print("The maximum length key value : " + str(res)) 

chevron_right


Output :

The original list is : [{‘best’: 2, ‘Gfg’: ‘abcd’}, {‘best’: 2, ‘Gfg’: ‘qwertyui’}, {‘good’: 3, ‘Gfg’: ‘xcvz’}, {‘good’: 4, ‘Gfg’: None}]
The maximum length key value : 8




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.