Sort Tuples Based on Second Parameter

Sort tuples based on second parameter

You can use the key parameter to list.sort():

my_list.sort(key=lambda x: x[1])

or, slightly faster,

my_list.sort(key=operator.itemgetter(1))

(As with any module, you'll need to import operator to be able to use it.)

How to sort a Tuple using two parameters?

Given:

>>> lot=[('John',32),('Jane',22),('Doe',32),('Mario',55)]

You can form a new tuple:

>>> sorted(lot, key=lambda t: (t[1],t[0]))
[('Jane', 22), ('Doe', 32), ('John', 32), ('Mario', 55)]

Or, in this case, you can reverse the tuple:

>>> sorted(lot, key=lambda t: t[::-1])
[('Jane', 22), ('Doe', 32), ('John', 32), ('Mario', 55)]

You can also use itemgetter with two arguments in the order you want the resulting key tuple to be:

>>> from operator import itemgetter
>>> sorted(lot, key=itemgetter(1,0))
[('Jane', 22), ('Doe', 32), ('John', 32), ('Mario', 55)]

sorting a set of tuples based on second value of tuple in python

Python sets are unordered, so you can’t sort them. But you can sort the elements in a set using the sorted function and passing a lambda that selects the second item of a tuple (since the set elements are tuples and you want to sort by the second elements of the tuples) to the key parameter. This returns a list:

out = sorted(b, key=lambda x:x[1])

Output:

[('bag', 0.67), ('leather', 0.77), ('shoe', 0.98)]

Sort a list of tuples by 2nd item (integer value)

Try using the key keyword with sorted().

sorted(
[('abc', 121), ('abc', 231), ('abc', 148), ('abc', 221)],
key=lambda x: x[1]
)

key should be a function that identifies how to retrieve the comparable element from your data structure. In your case, it is the second element of the tuple, so we access [1].

For optimization, see jamylak's response using itemgetter(1), which is essentially a faster version of lambda x: x[1].

Sort a list of tuples by second value, reverse=True and then by key, reverse=False

The following works with your input:

d = [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]
sorted(d,key=lambda x:(-x[1],x[0]))

Since your "values" are numeric, you can easily reverse the sort order by changing the sign.

In other words, this sort puts things in order by value (-x[1]) (the negative sign puts big numbers first) and then for numbers which are the same, it orders according to key (x[0]).

If your values can't so easily be "negated" to put big items first, an easy work-around is to sort twice:

from operator import itemgetter
d.sort(key=itemgetter(0))
d.sort(key=itemgetter(1),reverse=True)

which works because python's sorting is stable.

Sort a list of tuples depending on two elements

sorted(unsorted, key=lambda element: (element[1], element[2]))

I've assumed an order for the keys from the sample output.

How to sort a list/tuple of lists/tuples by the element at a given index?

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


Related Topics



Leave a reply



Submit