892

I have some data either in a list of lists or a list of tuples, like this:

data = [[1,2,3], [4,5,6], [7,8,9]]
data = [(1,2,3), (4,5,6), (7,8,9)]

And I want to sort by the 2nd element in the subset. Meaning, sorting by 2,5,8 where 2 is from (1,2,3), 5 is from (4,5,6). What is the common way to do this? Should I store tuples or lists in my list?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Stan
  • 37,207
  • 50
  • 124
  • 185
  • 79
    With regard to "Should I store tuples or lists in my list?", a rule of thumb is to make things as immutable as possible. If you don't *need* to modify the sublists in place, make them tuples. – Matthew Flaschen Jun 25 '10 at 23:10

11 Answers11

1479
sorted_by_second = sorted(data, key=lambda tup: tup[1])

or:

data.sort(key=lambda tup: tup[1])  # sorts in place

The default sort mode is ascending. To sort in descending order use the option reverse=True:

sorted_by_second = sorted(data, key=lambda tup: tup[1], reverse=True)

or:

data.sort(key=lambda tup: tup[1], reverse=True)  # sorts in place
user2314737
  • 27,088
  • 20
  • 102
  • 114
Stephen
  • 47,994
  • 7
  • 61
  • 70
  • 16
    Any idea how to sort it bigger to smaller? – billwild Dec 19 '12 at 13:57
  • 84
    @billwild : help(sorted). reverse=True. – Stephen Dec 20 '12 at 15:54
  • 46
    @Stephen using itemgetter is faster and simpler: `key=itemgetter(1)` and at the beginning of the file: `from operator import itemgetter` – Joschua Mar 13 '13 at 20:08
  • 3
    @Cemre as for the second example, `sort` here is a method of `List` object of Python, which receives a lambda function as its `key` parameter. You may name it as `tup`, or `t`, or whatever you like and it'll still work. `tup` here specifies index of the list's tuple, so `1` means that sorting will be performed by the second values of tuples from the original list (`2, 5, 8`). – Neurotransmitter Jul 26 '16 at 15:13
  • 5
    I was mildly sceptical of the unsubstantiated claim that "using itemgetter is faster and simpler." While I subjectively regard the intuitive `lambda` approach to be simpler than the unintuitive `itemgetter` class, `itemgetter` _does_ indeed [appear to be faster](https://stackoverflow.com/a/17243726/2809027). I'm curious as to why this is. My crude suspicion is that a `lambda` incurs the hidden cost of capturing all local variables into a closure context, whereas an `itemgetter` instance does not. _**tl;dr:** Always use `itemgetter`, because speed wins._ – Cecil Curry Nov 29 '16 at 06:02
  • 1
    I have done a more thorough benchmark between `lambda` and `itemgetter` used in sort [here](https://stackoverflow.com/a/53828002/6064933). `itemgetter` is always faster than `lambda`. – jdhao Dec 18 '18 at 08:36
  • What if have tuple [(2,'John'), (1, 'Simon'), (3, 'Rober')] and need to sort on - key1 ascending and key2 descending. Thanks. – sanjay patel Nov 20 '20 at 01:07
  • First sort on key2, then on key1. – Edouard Thiel Nov 08 '21 at 12:15
304
from operator import itemgetter
data.sort(key=itemgetter(1))
manova
  • 3,443
  • 1
  • 16
  • 8
  • 55
    _This should be the accepted answer._ See also [Charlie](https://stackoverflow.com/users/145976/charlie)'s [posted timings](https://stackoverflow.com/a/17243726/2809027), demonstrating the `itemgetter` class to sort **126% faster** on average than the equivalent `lambda` function. – Cecil Curry Nov 29 '16 at 06:07
  • 21
    You can also sort by multiple indices hierarchically, e.g. `data.sort(key=itemgetter(3,1))` – Michael Ohlrogge Jun 08 '17 at 17:29
91

For sorting by multiple criteria, namely for instance by the second and third elements in a tuple, let

data = [(1,2,3),(1,2,1),(1,1,4)]

and so define a lambda that returns a tuple that describes priority, for instance

sorted(data, key=lambda tup: (tup[1],tup[2]) )
[(1, 1, 4), (1, 2, 1), (1, 2, 3)]
elm
  • 20,117
  • 14
  • 67
  • 113
67

I just want to add to Stephen's answer if you want to sort the array from high to low, another way other than in the comments above is just to add this to the line:

reverse = True

and the result will be as follows:

data.sort(key=lambda tup: tup[1], reverse=True)
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
sifoo
  • 799
  • 5
  • 3
30

Stephen's answer is the one I'd use. For completeness, here's the DSU (decorate-sort-undecorate) pattern with list comprehensions:

decorated = [(tup[1], tup) for tup in data]
decorated.sort()
undecorated = [tup for second, tup in decorated]

Or, more tersely:

[b for a,b in sorted((tup[1], tup) for tup in data)]

As noted in the Python Sorting HowTo, this has been unnecessary since Python 2.4, when key functions became available.

Community
  • 1
  • 1
tcarobruce
  • 3,773
  • 21
  • 33
  • 2
    So this answer is useful for Python 2.3-? Are there any valid uses in more-current Python versions around which you might elaborate a bit? If not, no bother...was just passing by, saw this and the old noggin got to churning just a wee bit. Anyway, cheers and thanks for this walk back into the earlier days of Python. – mechanical_meat Mar 31 '12 at 08:21
29

In order to sort a list of tuples (<word>, <count>), for count in descending order and word in alphabetical order:

data = [
('betty', 1),
('bought', 1),
('a', 1),
('bit', 1),
('of', 1),
('butter', 2),
('but', 1),
('the', 1),
('was', 1),
('bitter', 1)]

I use this method:

sorted(data, key=lambda tup:(-tup[1], tup[0]))

and it gives me the result:

[('butter', 2),
('a', 1),
('betty', 1),
('bit', 1),
('bitter', 1),
('bought', 1),
('but', 1),
('of', 1),
('the', 1),
('was', 1)]
KernelPanic
  • 600
  • 8
  • 19
Ferris
  • 5,325
  • 1
  • 14
  • 23
16

Without lambda:

def sec_elem(s):
    return s[1]

sorted(data, key=sec_elem)
Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
Mesco
  • 1,254
  • 13
  • 21
11

itemgetter() is somewhat faster than lambda tup: tup[1], but the increase is relatively modest (around 10 to 25 percent).

(IPython session)

>>> from operator import itemgetter
>>> from numpy.random import randint
>>> values = randint(0, 9, 30000).reshape((10000,3))
>>> tpls = [tuple(values[i,:]) for i in range(len(values))]

>>> tpls[:5]    # display sample from list
[(1, 0, 0), 
 (8, 5, 5), 
 (5, 4, 0), 
 (5, 7, 7), 
 (4, 2, 1)]

>>> sorted(tpls[:5], key=itemgetter(1))    # example sort
[(1, 0, 0), 
 (4, 2, 1), 
 (5, 4, 0), 
 (8, 5, 5), 
 (5, 7, 7)]

>>> %timeit sorted(tpls, key=itemgetter(1))
100 loops, best of 3: 4.89 ms per loop

>>> %timeit sorted(tpls, key=lambda tup: tup[1])
100 loops, best of 3: 6.39 ms per loop

>>> %timeit sorted(tpls, key=(itemgetter(1,0)))
100 loops, best of 3: 16.1 ms per loop

>>> %timeit sorted(tpls, key=lambda tup: (tup[1], tup[0]))
100 loops, best of 3: 17.1 ms per loop
Walter
  • 397
  • 4
  • 11
  • Please see the itemgetter sorting solution for varying reverse arguments for multiple columns here, you then need to arrange your sorting in multiple steps in a row: https://stackoverflow.com/questions/14466068/sort-a-list-of-tuples-by-second-value-reverse-true-and-then-by-key-reverse-fal – questionto42 May 18 '20 at 16:00
7

@Stephen 's answer is to the point! Here is an example for better visualization,

Shout out for the Ready Player One fans! =)

>>> gunters = [('2044-04-05', 'parzival'), ('2044-04-07', 'aech'), ('2044-04-06', 'art3mis')]
>>> gunters.sort(key=lambda tup: tup[0])
>>> print gunters
[('2044-04-05', 'parzival'), ('2044-04-06', 'art3mis'), ('2044-04-07', 'aech')]

key is a function that will be called to transform the collection's items for comparison.. like compareTo method in Java.

The parameter passed to key must be something that is callable. Here, the use of lambda creates an anonymous function (which is a callable).
The syntax of lambda is the word lambda followed by a iterable name then a single block of code.

Below example, we are sorting a list of tuple that holds the info abt time of certain event and actor name.

We are sorting this list by time of event occurrence - which is the 0th element of a tuple.

Note - s.sort([cmp[, key[, reverse]]]) sorts the items of s in place

Rishi
  • 5,869
  • 7
  • 34
  • 45
1

I use this in my code:

#To sort the list based on each element's second integer (elem[1])
sorted(d2, key=lambda elem: elem[1])

Depending on which element you want to sort it by you can put it in the

(elem[*insert the index of the element you are sorting it by*])
-4

Sorting a tuple is quite simple:

tuple(sorted(t))
Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34
Jayr
  • 51
  • 1