How to Find the Shortest String in a Python List?

4.8/5 - (5 votes)

Use Python’s built-in min() function with a key argument to find the shortest string in a list. Call min(lst, key=len) to return the shortest string in lst using the built-in len() function to associate the weight of each string—the shortest string will be the minimum.

How to Find the Longest String in a Python List?

Problem Formulation

Given a Python list of strings. Find the string with the minimum number of characters—the shortest string in the list.

Here are a few example list of strings and the desired output:

# ['Alice', 'Bob', 'Pete']   ---->   'Bob'
# ['aaa', 'aaaa', 'aa']      ---->   'aa'
# ['']                       ---->   ''
# []                         ---->   ''

Solution: min() function with key function argument len()

Use Python’s built-in min() function with a key argument to find the shortest string in a list. Call min(lst, key=len) to return the shortest string in lst using the built-in len() function to associate the weight of each string—the shortest string will be the minimum.

Here’s the code definition of the get_min_str() function that takes a list of strings as input and returns the shortest string in the list or a ValueError if the list is empty.

def get_min_str(lst):
    return min(lst, key=len)

Here’s the output for our desired examples:

print(get_min_str(['Alice', 'Bob', 'Pete']))
# 'Bob'

print(get_min_str(['aaa', 'aaaa', 'aa']))
# 'aa'

print(get_min_str(['']))
# ''

print(get_min_str([]))
# ValueError

Border Case: What if the List is Empty?

If you want to return an alternative value in case the list is empty, you can modify the get_min_str() function to include a second optional argument:

def get_min_str(lst, fallback=''):
    return min(lst, key=len) if lst else fallback


print(get_min_str([]))
# ''

print(get_min_str([], fallback='NOOOOOOOOO!!!!!!'))
# NOOOOOOOOO!!!!!!

Solution with For Loop

A less Pythonic but, for beginner coders, more readable version is the following loop-based:

def get_min_str(lst, fallback=''):
    if not lst:
        return fallback

    min_str = lst[0]   # list is not empty

    for x in lst:
        if len(x) < len(min_str):
            min_str = x

    return min_str


print(get_min_str(['Alice', 'Bob', 'Pete']))
# 'Bob'

print(get_min_str(['aaa', 'aaaa', 'aa']))
# 'aa'

print(get_min_str(['']))
# ''

print(get_min_str([], fallback='NOOOOOOOOO!!!!!!'))
# NOOOOOOOOO!!!!!!

🌍 Related Tutorial: How to Find the Longest String in a Python List?

Python Min Length of String in List

To find the minimum length of a string in a given list, you can use the min(lst, key=len) function to obtain the string with the minimum length and then pass this min string into the len() function to obtain the number of characters of the min string.

len(min(lst, key=len))

Here’s a more detailed example:

def get_min_length(lst):
    return len(min(lst, key=len))


print(get_min_length(['Alice', 'Bob', 'Pete']))
# 3

print(get_min_length(['aaa', 'aaaa', 'aa']))
# 2

print(get_min_length(['']))
# 0

print(get_min_length([]))
# Value Error!!!

Python Find Shortest String in Set

To find the shortest string in a Python set, use Python’s built-in min() function with a key argument: min(my_set, key=len) returns the shortest string in my_set using the built-in len() function to determine the weight of each string—the shortest string will be the minimum.

Here’s the code definition of the get_min_str() function that takes a set of strings as input and returns the shortest string in the set or a ValueError if the set is empty.

def get_min_str(my_set):
    return min(my_set, key=len)


print(get_min_str({'Alice', 'Bob', 'Pete'}))
# 'Bob'

print(get_min_str({'aaa', 'aaaa', 'aa'}))
# 'aa'

print(get_min_str({''}))
# ''

print(get_min_str({}))
# ValueError: min() arg is an empty sequence

Regex Humor

Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)