How can I sort a list by a key described by an arbitrary function? For example, if I have:
mylist = [["quux", 1, "a"], ["bar", 0, "b"]]
I’d like to sort “mylist” by the second element of each member, e.g.
sort(mylist, key=lambda x: x[1])
how can I do this?
You basically have it already:
gives:
That will sort mylist in place.
[this para edited thanks to @Daniel’s correction.]
sortedwill return a new list that is sorted rather than actually changing the input, as described in http://wiki.python.org/moin/HowTo/Sorting/.