 GeeksforGeeks App
Open App Browser
Continue

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

## Python3

 `# 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))`

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

Time complexity: O(N), where N is the length of the input list test_list.
Auxiliary space: O(1), as the program uses constant extra space to store variables like test_list, filt_key, temp, res, etc., and the space required does not depend on the size of the input.

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

## Python3

 `# 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))`

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

Time complexity: O(n*n), where n is the number of elements in the test_list.
Auxiliary space: O(1), as program uses the constant space

Method #3: Using a for loop

Initialize a variable max_length to 0.
Traverse through each dictionary in the test_list using a for loop.
Check if the dictionary contains the given filter key.
If the dictionary contains the filter key, check if the value is not None.
If the value is not None and its length is greater than the current max_length, update max_length with the length of the value.
Finally, print the maximum length of the value for the given filter key.

## Python3

 `# Python3 code to demonstrate working of``# Maximum String value length of Key``# Using for loop` `# 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 for loop``max_length ``=` `0``for` `sub_dict ``in` `test_list:``    ``if` `filt_key ``in` `sub_dict:``        ``value ``=` `sub_dict[filt_key]``        ``if` `value ``is` `not` `None` `and` `len``(value) > max_length:``            ``max_length ``=` `len``(value)` `# printing result``print``(``"The maximum length key value : "` `+` `str``(max_length))`

Output

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

The time complexity of this approach is O(n), where n is the length of the test_list.

The auxiliary space required is O(1), as we are only using a constant amount of extra memory to store the max_length variable.

My Personal Notes arrow_drop_up