Python | Longest String in list

Sometimes, while working with Python Lists, we can have a problem in which we receive Strings as elements and wish to compute the String which has maximum length. This kind of problem can have applications in many domains. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using loop
This is the brute method in which we perform this task. In this, we run a loop to keep a memory of longest string length and return the string which has max length in list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Longest String in list
# using loop
  
# initialize list 
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
  
# printing original list 
print("The original list : " + str(test_list))
  
# Longest String in list
# using loop
max_len = -1
for ele in test_list:
    if len(ele) > max_len:
        max_len = len(ele)
        res = ele
  
# printing result
print("Maximum length string is : " + res)

chevron_right


Output :

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Maximum length string is : geeks

 

Method #2 : Using max() + key
This method can also be used to solve this problem. In this, we use inbuilt max() with “len” as key argument to extract the string with the maximum length.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Longest String in list
# using max() + key
  
# initialize list 
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
  
# printing original list 
print("The original list : " + str(test_list))
  
# Longest String in list
# using max() + key
res = max(test_list, key = len)
  
# printing result
print("Maximum length string is : " + res)

chevron_right


Output :

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Maximum length string is : geeks



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.