Print Shortest & Longest String in List in Python (3 Examples)

 

In this article, you’ll learn how to print the shortest and longest strings in a list using the Python language.

Take a look at the structure of the tutorial:

Let’s go straight to the code!

 

Create Sample Data

We will create a sample Python list as follows.

my_list = ['apple', 'banana','watermelon', 
            'orange','kiwi']
print(my_list)
# ['apple', 'banana', 'watermelon', 
# 'orange', 'kiwi']

As you can see, we have created my_list, a list object that contains different strings. How can we print the longest and the shortest strings in my_list? Let’s take a look at different examples!

 

Example 1: Print Shortest & Longest String via min() and max() Functions

This example consists of using the max() and min() functions as follows.

shortest_str = min(my_list, key=len)
longest_str = max(my_list, key=len)
 
print(f"The shortest string is: {shortest_str}")
print(f"The longest string is: {longest_str}")
# The shortest string is: kiwi
# The longest string is: watermelon

As shown above, this method uses the built-in min() and max() functions to find the shortest and longest strings in my_list. The key=len argument specifies that the length of each string should be used as the basis for comparison. Then, the resulting strings are assigned to the ‘shortest_str’ and ‘longest_str’ variables, which are printed using f-strings.

 

Example 2: Print Shortest & Longest String via for Loop

This second example uses a for loop to iterate over my_list and keep track of the shortest and longest strings.

shortest_str = my_list[0]
longest_str = my_list[0]
 
for item in my_list:
    if len(item) < len(shortest_str):
        shortest_str = item
    elif len(item) > len(longest_str):
        longest_str = item
 
print(f"The shortest string is: {shortest_str}")
print(f"The longest string is: {longest_str}")
# The shortest string is: kiwi
# The longest string is: watermelon

As shown, this method initializes the shortest_str and longest_str variables as equal to the first string in my_list. Then, the loop iterates over the elements in my_list, comparing the length of each string to the lengths of shortest_str and longest_str. If shorter or longer strings are found, they are assigned to shortest_str or longest_str respectively. Finally, the shortest and longest strings are printed using f-strings.

 

Example 3: Print Shortest & Longest String via sorted() Function

In this last method, we will use the sorted() function and the key=len parameter to print the longest and shortest strings in my_list. Let’s take a look!

sorted_list = sorted(my_list, key=len)
 
shortest_str = sorted_list[0]
longest_str = sorted_list[-1]
 
print(f"The shortest string is: {shortest_str}")
print(f"The longest string is: {longest_str}")
# The shortest string is: kiwi
# The longest string is: watermelon

As you can see, the sorted() function is used to sort my_list based on the length of each string. The key parameter is set to len, which tells Python to use the len() function to determine the sorting order.

The longest string is the last item in the sorted list, and the first item is the shortest. Thus, the sorted list is then assigned to the shortest_str variable using sorted_list[0], and the longest string is assigned to the longest_str variable using sorted_list[-1] with negative indexing.

We finally print the shortest and longest strings using f-strings and the print() function!

 

Video, Further Resources & Summary

Do you need more explanations on how to print the longest and shortest strings in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at the related available tutorials on Statistics Globe:

This post has shown how to print the shortest and longest strings in a list in Python. In case you have further questions, please let me know in the comments section.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top