Sorting a list
John Salerno
johnjsal at NOSPAMgmail.com
Thu Feb 1 16:41:02 EST 2007
Bruno Desthuilliers wrote:
>> One more thing. What if I want them in reverse chronological order? I
>> tried reverse() but that seemed to put them in reverse alphabetical
>> order based on the second element of the tuple (not the year).
>
> Really ?
>
> >>> lines = [('1995', 'aaa'), ('1997', 'bbb'), ('1995', 'bbb'),
> ('1997', 'aaa'), ('1995', 'ccc'), ('1996', 'ccc'), ('1996', 'aaa')]
> >>> lines.sort()
> >>> lines
> [('1995', 'aaa'), ('1995', 'bbb'), ('1995', 'ccc'), ('1996', 'aaa'),
> ('1996', 'ccc'), ('1997', 'aaa'), ('1997', 'bbb')]
> >>> lines.reverse()
> >>> lines
> [('1997', 'bbb'), ('1997', 'aaa'), ('1996', 'ccc'), ('1996', 'aaa'),
> ('1995', 'ccc'), ('1995', 'bbb'), ('1995', 'aaa')]
> >>>
Oh I didn't sort then reverse, I just replaced sort with reverse. Maybe
that's why!
More information about the Python-list
mailing list