sorting a list of tupples

Dave Kuhlman dkuhlman at rexx.com
Thu Oct 30 22:44:35 EST 2003


John Roth wrote:

> 
> "David Bear" <david.bear at asu.edu> wrote in message
> news:m3ptgegrp6.fsf at moroni.pp.asu.edu...
>> 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?
> 
> <list>.sort() should do the job, as long as you want the result
> to be a list in ascending order by the value of the first element
> of the tuple.

And, if you don't want to sort on the first element or want to
sort in descending order or ... you will want to know about the
optional argument to the sort method.  You can pass it your own
custom comparison function. Read about it here:

    http://www.python.org/doc/current/lib/typesseq-mutable.html

The info you (might) want is down in a footnote.

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list