Sorting a List of Lists

Létezo letezo at fw.hu
Tue Jan 30 19:17:00 EST 2007


> events = [['Event URL as String', 'Event Title as String ', Event Date
> as Datetime], ...]
>
> I then thought I'd just go events.sort(lambda x,y: x[2]<y[2]) and call
> it a day. That didn't work. But then lamda functions like to be very
> simple, maybe object subscripts aren't allowed (even though I didn't
> get an error). So I wrote a comparison function that looks much as you
> would expect:

Comparision functions must return -1, 0 or 1, not a bool.
You may use a key function instead in this case (requires python 2.4 or 
newer):

events.sort(key=lambda x: x[2])

Viktor 




More information about the Python-list mailing list