Find Shortest Word in List Python

Find Shortest Word in List Python | When working with a large amount of data, we may encounter a problem where we need to retrieve the shortest string from a list of strings. This type of problem has a wide range of applications.

Let’s look at a few different approaches to find the shortest word in List python. To discover the smallest of a set of numbers In Python, create a list of strings depending on length, call the built-in function min(), send the strings to evaluate as parameters to min(), and also pass the len() function as a key argument.

Based on the value, the min() method returns the string having the shortest length. In this article, we’ll look at sample programs on how to find the shortest string in a list in python. Let us look at the following example.

Example: Consider a list of pet animals stored in the form of strings. We will find shortest string in list python.

Python Find Shortest String in List Using min()

myPets = ['cats', 'birds', 'fishes', 'dog']
result = min(myPets, key=len)
print(f'The shortest word in list Python is - {result}.')

Output:-

The shortest word in list Python is – dog.

Moving forward, we have two methods where we can use python to find the shortest string in the list. 

Find Shortest Word in List Python Using min() Function

# initialize list
List = {'Java', 'Python', 'PHP', 'JavaScript'}

# shortest string length
# using min() + key
result = min(List, key=len)

# result
print(f'String with least length is - {result}.')

Output:-

String with least length is – PHP.

The program is given to the min() method with the key=len keyword parameter, which returns the smallest word from the input. And all of this may be accomplished in a single line.

Python Find Shortest String in List Using Loops

It is the force method of completing this task. We execute a loop to keep the count of the shortest length of the string and return the string with the shortest length in the list.

# find shortest string in list python using loop
# initializing list
List = {'Java', 'Python', 'PHP', 'JavaScript', 'Go', ' Ruby'}

# the shortest string in a list python
# using loop
min_len = 30
for ele in List:
    if len(ele) < min_len:
        min_len = len(ele)
        res = ele

# output
print("String with least length is : " + res)

Output:-

String with least length is: PHP

Steps:

1. Assuming the item with the smallest value will be the first.
2. The use of a for loop as well as an if statement, the length of the input in the list are evaluated with the first element.
3. Save the name of the word with the least length in a temporary variable.
4. The term with the smallest length should be printed.
5. The end of the program.

In this Python tutorial, we learned how to find the shortest string in a list python. We hope you liked the concepts! Also see:- Find the Last Digit in String Python

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.