How to sort a list/tuple of lists/tuples by the element at a given index?
Solution 1
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
Solution 2
from operator import itemgetter
data.sort(key=itemgetter(1))
Solution 3
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)]
Solution 4
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)
Solution 5
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)]
Stan
Updated on July 21, 2022Comments
-
Stan almost 2 years
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?-
Matthew Flaschen almost 14 yearsWith 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.
-
-
mechanical_meat about 12 yearsSo 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.
-
billwild over 11 yearsAny idea how to sort it bigger to smaller?
-
Stephen over 11 years@billwild : help(sorted). reverse=True.
-
Joschua about 11 years@Stephen using itemgetter is faster and simpler:
key=itemgetter(1)
and at the beginning of the file:from operator import itemgetter
-
Neurotransmitter almost 8 years@Cemre as for the second example,
sort
here is a method ofList
object of Python, which receives a lambda function as itskey
parameter. You may name it astup
, ort
, or whatever you like and it'll still work.tup
here specifies index of the list's tuple, so1
means that sorting will be performed by the second values of tuples from the original list (2, 5, 8
). -
Cecil Curry over 7 yearsI 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 unintuitiveitemgetter
class,itemgetter
does indeed appear to be faster. I'm curious as to why this is. My crude suspicion is that alambda
incurs the hidden cost of capturing all local variables into a closure context, whereas anitemgetter
instance does not. tl;dr: Always useitemgetter
, because speed wins. -
Cecil Curry over 7 yearsThis should be the accepted answer. See also Charlie's posted timings, demonstrating the
itemgetter
class to sort 126% faster on average than the equivalentlambda
function. -
Michael Ohlrogge almost 7 yearsYou can also sort by multiple indices hierarchically, e.g.
data.sort(key=itemgetter(3,1))
-
jdhao over 5 yearsI have done a more thorough benchmark between
lambda
anditemgetter
used in sort here.itemgetter
is always faster thanlambda
. -
eric over 4 yearswhat if tup[1] is a string?
-
questionto42standswithUkraine almost 4 yearsPlease 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: stackoverflow.com/questions/14466068/…
-
sanjay patel over 3 yearsWhat if have tuple [(2,'John'), (1, 'Simon'), (3, 'Rober')] and need to sort on - key1 ascending and key2 descending. Thanks.
-
Shital Shah over 2 years
sorted
creates new list. To do in-place sorting use.sort(key=...)
-
Edouard Thiel over 2 yearsFirst sort on key2, then on key1.