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).
The
minfunction has an optional parameterkeythat lets you specify a function to determine the “sorting value” of each item. We just need to set this to thelenfunction to get the shortest value: