sorting a list of tupples
Daniel Luz
whyyou at wanna.no
Thu Oct 30 20:22:39 EST 2003
David Bear wrote:
> I have a list of tupples. Each tupple has 3 items. The first item is
> an integer. I'd like to sort the list(of tupples), based on the first
> element of the tupple. I thought about stringifying the tupples, then sorting
> those, but thought there must be a better way. any idea's?
Have you tried just sort()ing the list? Tuples naturally will sort based
on the first element of them (then, if two elements are equal, it will
try to sort based on the second element, and so forth).
>>> l = [(1, 'p'), (6, 'n'), (4, 'h'), (3, 't'), (5, 'o'), (2, 'y')]
>>> l.sort()
>>> l
[(1, 'p'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o'), (6, 'n')]
If that's not what you meant, could you please clarify?
--
Daniel
More information about the Python-list
mailing list