sorting a list of tupples

Stephen Horne steve at ninereeds.fsnet.co.uk
Thu Oct 30 20:53:19 EST 2003


On 30 Oct 2003 17:45:57 -0700, David Bear <david.bear at asu.edu> 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?

You can sort a list of tuples using the standard sort method. Ordering
of tuples is derived much like ordering of strings - it will use the
order of the first items in the tuples unless they are equal in which
case it uses the second items, or third items, or whatever. If all
else fails, the longest tuple is considered greater.

If you are sorting on the first item, the sort method should just work
for you.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list