Python | Extract length of longest string in list

Sometimes, while working with a lot of data, we can have a problem in which we need to extract the maximum length of all the strings in list. This kind of problem can have application in many domains. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using max() + generator expression
The combination of above functionalities can be used to perform this task. In this, we extract all the list lengths using generator expression and return maximum of them using max().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extracting length of longest string in list
# using max() + generator expression
  
# initialize list 
test_list = ['gfg', 'is', 'best']
  
# printing original list 
print("The original list : " + str(test_list))
  
# Extracting length of longest string in list
# using max() + generator expression
res = max(len(ele) for ele in test_list)
  
# printing result
print("Length of maximum string is : " + str(res))

chevron_right


Output :



The original list : ['gfg', 'is', 'best']
Length of maximum string is : 4

 

Method #2 : Using len() + key argument + max()
The combination of above functions can be used to perform this task. In this, we extract the maximum length using len() and max(). It is faster than above method as it performs more task built in rather than overhead by generator expression.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extracting length of longest string in list
# using len() + key argument + max()
  
# initialize list 
test_list = ['gfg', 'is', 'best']
  
# printing original list 
print("The original list : " + str(test_list))
  
# Extracting length of longest string in list
# using len() + key argument + max()
res = len(max(test_list, key = len))
  
# printing result
print("Length of maximum string is : " + str(res))

chevron_right


Output :

The original list : ['gfg', 'is', 'best']
Length of maximum string is : 4


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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.