468,765 Members | 1,183 Online

# sorting a list of tupples

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?

--David Bear
phone: 480-965-8257
fax: 480-965-9189
College of Public Programs/ASU
Wilson Hall 232
Tempe, AZ 85287-0803
"Beware the IP portfolio, everyone will be suspect of trespassing"
Jul 18 '05 #1
4 4509

"David Bear" <da********@asu.edu> wrote in message
news:m3************@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.

John Roth

--David Bear
phone: 480-965-8257
fax: 480-965-9189
College of Public Programs/ASU
Wilson Hall 232
Tempe, AZ 85287-0803
"Beware the IP portfolio, everyone will be suspect of trespassing"

Jul 18 '05 #2
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

Jul 18 '05 #3
On 30 Oct 2003 17:45:57 -0700, David Bear <da********@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
Jul 18 '05 #4
John Roth wrote:

"David Bear" <da********@asu.edu> wrote in message
news:m3************@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

http://www.python.org/doc/current/li...q-mutable.html

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

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dk******@rexx.com
Jul 18 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.