How to find the shortest string in a list in Python

Python

Python Problem Overview


This seems like a pretty simple problem, but I'm looking for a short and sweet way of doing it that is still understandable (this isn't code golf).

Given a list of strings, what's the easiest way to find the shortest string?

The way that is most obvious to me is roughly:

l = [...some strings...]
lens = map(l, len)
minlen, minind = min(lens)
shortest = l[minind]

but that seems like a lot of code for this problem (at least in python).

Python Solutions


Solution 1 - Python

The min function has an optional parameter key that lets you specify a function to determine the "sorting value" of each item. We just need to set this to the len function to get the shortest value:

strings = ["some", "example", "words", "that", "i", "am", "fond", "of"]

print min(strings, key=len) # prints "i"

Solution 2 - Python

I'd use sorted(l, key=len)[0]

Solution 3 - Python

Takes linear time:

   reduce(lambda x, y: x if len(x) < len(y) else y, l)

Solution 4 - Python

Potential answer:

l = [...some strings...]
l.sort(key=len)
shortest = l[0]

However, this is probably very inefficient in that it sorts the entire list, which is unnecessary. We really just need the minimum.

Solution 5 - Python

As suggested in other answers, these solutions takes linear time. They need need to be guarded against empty iterables though:

import functools

strings = ["small str", "xs", "long string"]

if (strings):
    print( "shortest string:", functools.reduce(lambda x, y: x if len(x) < len(y) else y, strings) )
    # or if you use min:
    # print( "shortest string:", min(strings, key=len) )
else:
    print( "list of strings is empty" )

Solution 6 - Python

To find the shortest string in a list:

str = ['boy', 'girl', 'lamb', 'butterfly']

def len_str(string):
  return len(string)

def compare_len(list):
  x = []
  for i in range(len(list)):
    x.append(len_str(list[i]))
  return min(x) #change to max if you're looking for the largest length

compare_len(str)

Solution 7 - Python

arr=('bibhu','prasanna','behera','jhgffgfgfgfg')
str1=''

#print (len(str))
for ele in arr:
    print (ele,ele[::-1])
    if len(ele)>len(str1):
        str1=ele
    elif len(ele)<len(str2):
        str2=ele
print ("the longest element is :",str1)
str2=arr[0]
for ele in arr:
    if len(ele)<len(str2):
        str2=ele

print ("the shortest element is :",str2) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleecbakerView Question on Stackoverflow
Solution 1 - PythonJeremyView Answer on Stackoverflow
Solution 2 - PythoncarlpettView Answer on Stackoverflow
Solution 3 - Pythonamit kumarView Answer on Stackoverflow
Solution 4 - PythonleecbakerView Answer on Stackoverflow
Solution 5 - PythonPedro LopesView Answer on Stackoverflow
Solution 6 - PythonGiftView Answer on Stackoverflow
Solution 7 - PythonbibhuView Answer on Stackoverflow