Use Python’s built-in min() function with the key argument to find the shortest string in a set. Call min(my_set, key=len) to return the shortest string in the set using the built-in len() function to determine the weight of each string—the shortest string has minimum length.
♥️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
👉 Recommended Tutorial: How to Find the Shortest String in a Python List?
Problem Formulation
Given a Python set of strings. Find the string with the minimum number of characters—the shortest string in the set.
Here are a few example sets of strings and the desired output:
# {'Alice', 'Bob', 'Pete'} ----> 'Bob'
# {'aaa', 'aaaa', 'aa'} ----> 'aa'
# {''} ----> ''
# {} ----> ''Method 1: min() Function with Key Argument Set to len()
Use Python’s built-in min() function with a key argument to find the shortest string in a set like so: min(s, key=len). This returns the shortest string in the set s 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 list or a ValueError if the set is empty.
def get_min_str(my_set):
return min(my_set, key=len)Here’s the output we obtain when running our desired examples:
print(get_min_str({'Alice', 'Bob', 'Pete'}))
# 'Bob'
print(get_min_str({'aaa', 'aaaa', 'aa'}))
# 'aa'
print(get_min_str({''}))
# ''If you pass an empty set, Python will raise a ValueError: min() arg is an empty sequence because you cannot pass an empty iterable into the min() function.
print(get_min_str({}))
# ValueError: min() arg is an empty sequenceMethod 2: Handling Empty Sets
If you want to return an alternative value in case the set is empty, you can modify the get_min_str() function to include a second optional argument:
def get_min_str(my_set, fallback=''):
return min(my_set, key=len) if my_set else fallback
print(get_min_str({}))
# ''
print(get_min_str({}, fallback='EMPTY!!!!!!'))
# EMPTY!!!!!!Method 3: Not-So-Pythonic with For Loop
A less Pythonic but, for beginner coders, more readable version is the following loop-based solution:
def get_min_str(my_set, fallback=''):
if not my_set:
return fallback
min_str = '' # set is not empty
for x in my_set:
if len(x) < len(max_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='EMPTY!!!!!!'))
# EMPTY!!!!!!Method 4: Python Min Length of String in Set
To find the minimum length of a string in a given set, use the min(my_set, 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.
len(min(my_set, key=len))Here’s a more detailed example:
def get_min_str_length(my_set):
return len(max(my_set, key=len))
print(get_min_str_length({'Alice', 'Bob', 'Pete'}))
# 3
print(get_min_str_length({'aaa', 'aaaa', 'aa'}))
# 2
print(get_min_str_length({''}))
# 0
print(get_min_str_length({}))
# ValueError: min() arg is an empty sequence
Thanks for taking the time to read this article! 🙂 Feel free to join us – it’s fun and we have cheat sheets!
👉 Recommended Tutorial: How to Get the Maximum String in a Python Set?