Python – Find Shortest String in a Set

To find the shortest string in a Set in Python, you can use Python min() built-in function. min() built-in function takes the set of strings as first argument, key function of len() as second argument, and returns the shortest string in the set.

The syntax of the function call to min(), to find the shortest string in the set s is

min(s, key=len)


In the following program, we initialize a Python Set aSet with strings and find the shortest of the strings (string with least length) in the set using min() function.


# initialize set
aSet = {'apple', 'fig', 'banana'}

# find the shortest string
shortest = min(aSet, key=len)

print(f'shortest string : {shortest}')
Try Online


shortest string : fig



In this Python Tutorial, we learned how to find the shortest string in a set, with the help of example programs.