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.

## 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

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.