-2

Say I have the following list:

mylist=[((0, 2), 4),
        ((0, 3), 9),
        ((0, 7), 49),
        ((0, 17), 50),
        ((0, 67), 85),
        ((0, 77), 98),
        ((1, 2), 1),
        ((1, 3), 4),
        ((1, 4), 9)]

How can I sort it based on the integer, stand-alone value within each tuple? Said values are, in the example, 4, 9, 49, and so forth.

The result should be something like this:

  mylist=[((1, 2), 1)
            ((0, 2), 4),
            ((1, 3), 4),
            ((0, 3), 9),
            ((1, 4), 9)
            ((0, 7), 49),
            ((0, 17), 50),
            ((0, 67), 85),
            ((0, 77), 98)]

The order in which the (x,y) values in each tuple are sorted is irrelevant.

FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • Use `itemgetter(1)`, as shown in Jamylak's answer in the linked duplicate question. – PM 2Ring Jul 05 '16 at 09:52
  • Wow, all of these down votes are great! Must have said something to be ashamed of. You are all ready to point your finger. A deeply sad jury, that's what you are. – FaCoffee Jul 05 '16 at 15:57
  • You probably got those downvotes from people who considered that you didn't do adequate research on this question; I was not one of them. – PM 2Ring Jul 05 '16 at 16:05

1 Answers1

2

Use sorted with a key lambda:

sorted_list = sorted(mylist, key=lambda x: x[1])
DeepSpace
  • 78,697
  • 11
  • 109
  • 154