Find The Longest String In a List Python

Find The Longest String In a List Python | Here, in the article, we will discuss how to find the longest string in a list in Python. Firstly let us see what is a list of strings. 

Python List: A list is an ordered data structure with elements separated by a comma and enclosed within square brackets. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data.

Python List Example: For example, you want to create a list to store the first five odd numbers. Odd= [1,3,5,7,9]. Here [1,3,5,7,9] are the list items of the list named Odd.

And String is generally a sequence of characters. Python does not have a character data type, a single character is simply a string with a length of 1. For example, “world” is a string including a sequence of characters ‘w’, ‘o’, ‘r’, ‘l’, and ‘d’. We use single quotes or double quotes to denote a string in Python.

We can find the longest string in a list in python in the following ways:-

  • Using max() Function
  • Using loops

Longest String In a List Python Using max() Function

It is one of the simplest ways to find the longest string in a list python. This involves built-in max() function. The max() function takes two arguments, the iterable and other argument is key. The key is the length to extract the string with the maximum length.

# input list
list = ['apple', 'banana', 'kiwi', 'pineapple', 'mango']
print("List of strings: ", list)

longest_string = max(list, key=len)
print("Longest string in list: ", longest_string)

Output:-

List of strings: [‘apple’, ‘banana’, ‘kiwi’, ‘pineapple’, ‘mango’]
Longest string in list: pineapple

If the given list is empty i.e. doesn’t contain any element then it will give ValueError: max() arg is an empty sequence. The below program demonstrates it:-

# input list
list = []
print("List of strings: ", list)

longest_string = max(list, key=len)
print("Longest string in list: ", longest_string)

Output:-

List of strings: [ ]
Traceback (most recent call last):
File “test.py”, line 5, in
longestString = max(list, key=len)
ValueError: max() arg is an empty sequence

Python Find Longest String In List using Loops

It simply uses for loop to repeat over the elements of the provided list. It checks the length of each string element and returns the string of maximum length.

# input list
list = ['guava', 'banana', 'apple', 'orange']
print("List of strings:", list)

max_len = -1
for ele in list:
    if (len(ele) > max_len):
        max_len = len(ele)
        longest_string = ele

print("Longest string in list: ", longest_string)

Output:-

List of strings: [‘guava’, ‘banana’, ‘apple’, ‘orange’]
Longest string in list: banana

If the list is empty then the below program will return an empty string. Since the list is blank therefore loop will not be able to iterate it and therefore no elements will be assigned to the longest_string variable.

# input list
list = []
print("List of strings:", list)

max_len = -1
longest_string = ''
for ele in list:
    if (len(ele) > max_len):
        max_len = len(ele)
        longest_string = ele

print("Longest string in list: ", longest_string)

So, here we have discussed how to find the longest string in a Python List using for loop and max() functions.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *