I have a list
list1 = [[a, e, o, i, u],
[3.0, 2.0, 1.0, 5.0, 4.0],
[5.1, 4.7, 2.5, 3.2, 1.1]]
I want to sort this list based on the 2nd list. i.e. the middle one. The result should be as follows
sorted_list1 = [[o, e, a, u, i],
[1.0, 2.0, 3.0, 4.0, 5.0],
[2.5, 4.7, 5.1, 1.1, 3.2]
Please mention the supporting import modules as well if required.
Note: I have equal number of elements in each list.
zip(*sorted(zip(*list1), key=lambda c: c[1]))should do this (transpose, sort on middle column, transpose again). You may need to addlist()in Python 3, or wrap in[list(r) for r in ...]to output a list of lists again.[*map(list, zip(*sorted(zip(*list1), key=lambda c: c[1])))]if you really need a list of lists instead of a list of tuples.