2

So I have this list, we'll call it listA. I'm trying to get the [3] item in each list e.g. ['5.01','5.88','2.10','9.45','17.58','2.76'] in sorted order. So the end result would start the entire list over again with Santa at the top. Does that make any sense?

[['John Doe', u'25.78', u'20.77', '5.01'], ['Jane Doe', u'21.08', u'15.20', '5.88'], ['James Bond', u'20.57', u'18.47', '2.10'], ['Michael Jordan', u'28.50', u'19.05', '9.45'], ['Santa', u'31.13', u'13.55', '17.58'], ['Easter Bunny', u'17.20', u'14.44', '2.76']]

3
  • Use itemgetter() from the operator module. It is not well know, but is designed to do exactly what you want. It is easier to read than lambdas and very simple to implement. Commented Dec 4, 2012 at 5:26
  • @MichaelDavidWatson, unfortunately itemgetter won't help with sorting the strings numerically. Commented Dec 4, 2012 at 6:02
  • True. But it is still good to be aware of itemgetter for everyone that comes to this page from google looking for a similar solution where the values are already float/int Commented Dec 4, 2012 at 16:41

4 Answers 4

6

If you want to return the complete list in sorted order, this may work. This takes your input list and runs sorted on top of it. The reverse argument set to True sorts the list in reverse (descending) order, and the key argument specifies the method by which to sort, which in this case is the float of the third argument of each list:

In [5]: l = [['John Doe', u'25.78', u'20.77', '5.01'] # continues...    
In [6]: sorted(l, reverse=True, key=lambda x: float(x[3]))
Out[6]:
[['Santa', u'31.13', u'13.55', '17.58'],
 ['Michael Jordan', u'28.50', u'19.05', '9.45'],
 ['Jane Doe', u'21.08', u'15.20', '5.88'],
 ['John Doe', u'25.78', u'20.77', '5.01'],
 ['Easter Bunny', u'17.20', u'14.44', '2.76'],
 ['James Bond', u'20.57', u'18.47', '2.10']]

If you only need the values in sorted order, the other answers provide viable ways of doing so.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

>>> listA=[['John Doe', u'25.78', u'20.77', '5.01'], ['Jane Doe', u'21.08', u'15.20', '5.88'], ['James Bond', u'20.57', u'18.47', '2.10'], ['Michael Jordan', u'28.50', u'19.05', '9.45'], ['Santa', u'31.13', u'13.55', '17.58'], ['Easter Bunny', u'17.20', u'14.44', '2.76']]

>>> [x[3] for x in sorted(listA, reverse = True, key = lambda i : float(i[3]))]
['17.58', '9.45', '5.88', '5.01', '2.76', '2.10']

Comments

1

An anonymous lambda function is not necessary for this task. You can use operator.itemgetter, which may be more intuitive:

from operator import itemgetter

res1 = sorted(map(itemgetter(3), listA), reverse=True, key=float)

['17.58', '9.45', '5.88', '5.01', '2.76', '2.10']

Comments

0
from operator import itemgetter
lstA.sort(reverse = True, key = itemgetter(3))

And you are good to go. Using itemgetter() within a sort is extremely helpful when you have multiple indexs to sort on. Lets say you wanted to sort alphabetically on the first value in case of a tie you could do the following

from operator import itemgetter
lstA.sort(key = itemgetter(0))
lstA.sort(reverse = True, key = itemgetter(3))

And Voilà!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.