0

Lets say that I have a list of 3 tuples:

[(x, 5), (y, 10), (z, 7)]

I'd like to rearrange the listing of the items based on the 2nd element of each tuple from largest to smallest so the resulting list can look something like this:

[(y, 10), (z, 7), (x,5)]

I'm not as fluent in lists than other programming aspects in Python and I find resolving this difficult.

How can this be solved?

1 Answers1

3

Just use .sort with a key function.

my_list.sort(key=lambda x: x[1], reverse=True)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895