Sort multidimensional list in python

Question:

I have a below list and I want to sort the 0th index of each row in ascending order. Input is as follow-

   [[[41, 34, 45, 55, 63], 'A'],
   [[42, 23, 34, 44, 53], 'B'],
   [[32, 23, 13, 54, 67], 'C'],
   [[23, 82, 23, 63, 34], 'D'],
   [[21, 23, 25, 56, 56], 'E']]]
   

The output should be :-

   [[[34, 41, 45, 55, 63], 'A'],
   [[23, 34, 42, 44, 53], 'B'],
   [[13, 23, 32, 54, 67], 'C'],
   [[23, 23, 34, 63, 84], 'D'],
   [[21, 23, 25, 56, 56], 'E']]]
  

I tried to sort by using the sorted() but I am not getting desired output.

Asked By: Raman Kumar Parida

||

Answers:

 a = [[[41, 34, 45, 55, 63], 'A'],
   [[42, 23, 34, 44, 53], 'B'],
   [[32, 23, 13, 54, 67], 'C'],
   [[23, 82, 23, 63, 34], 'D'],
   [[21, 23, 25, 56, 56], 'E']]

[[sorted(row[0]), row[1]] for row in a]
Answered By: Gal Moran
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.