How to sort a List of Lists in Python

Hello All!

I wanted to know how to sort a list of lists in Python by the given index of every list.

Suppose we have a list that contains 4 other lists each having 3 elements. How to sort the list by the second element of each of the inner list?

Example:

list = [[1, 2, 3], [4, 5, 6], [4, 1, 7], [6, 3, 11]]

After sorting by second element, the resultant list will be:

list = [[4, 1, 7], [1, 2, 3], [6, 3, 11], [4, 5, 6]]

You can use comparators
sorted_list = sorted(arr,key = lambda x: x[1])

1 Like