How to find the shortest string in a list in Python

后端 未结 6 684
星月不相逢
星月不相逢 2020-12-02 19:53

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

6条回答
  •  心在旅途
    2020-12-02 20:33

    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.

提交回复
热议问题