How to sort a list of strings on a substring

Steven D'Aprano steven at
Tue Oct 6 04:35:14 CEST 2009

On Mon, 05 Oct 2009 15:45:58 -0700, n00m wrote:

> Here you are:
> LogList = [\
>     "inbound tcp office inside 88", "inbound tcp
>     office inside 2967", "inbound udp lab
> inside 161", "inbound udp office
>     inside 53"]
> LogList.sort(key=lambda x: x[x.index('1'):])

No, that's incorrect. Try it with this data and you will see it fails:

LogList = [
"inbound tcp office1 inside 88",
"inbound tcp office2 inside 2967",
"inbound udp lab1 inside 161",
"inbound udp office2 inside 53",
"inbound udp lab2 inside 161",
"inbound udp webby inside 54",

Worse, if you delete the last item ("webby"), the code silently does the 
wrong thing. Code that crashes is bad, but code that silently does the 
wrong thing is a nightmare. Your test succeeded by accident -- it was a 
fluke of the data that you failed to see both failure modes.

The question asked was how to sort the list according to item 3 of the 
strings, *not* how to sort the list according to the first character '1'. 
The way to solve this correctly is by extracting item 3 and sorting on 
that, not by searching for the first character '1'. That is a hack[1] 
that just happened to work for the specific test data you tried it on.

[1] Hack in the bad sense, not in the good sense.


More information about the Python-list mailing list