Sort a List of Lists in Python

  1. Use the itemgetter() Function From the Operator Module Along With the sorted() Function to Sort a List of Lists in Python
  2. Use the lambda Expression Along With the sorted() Function to Sort a List of Lists in Python
  3. Use the sort() Function to Sort a List of Lists in Python

A list is one of the most powerful data structures used in Python. We can sort a list in Python by arranging all its elements in ascending or descending order based on the requirement.

We can also have nested lists in Python. These can be thought of as a list of lists. Sorting a list of lists arranges all the inner lists according to the specified index as the key.

In this tutorial, we will sort a list of lists in Python based on some indexes.

Use the itemgetter() Function From the Operator Module Along With the sorted() Function to Sort a List of Lists in Python

The function sorted() is used to sort a list in Python. By default, it sorts the list in ascending order. The function itemgetter() from the operator module takes an index number as a parameter and returns the element from the data set placed at that index number.

Therefore, the function sorted(List_name, key=itemgetter(index_number)) sorts a list of lists by the element positioned at the specified index_number of each inner list.

For example,

from operator import itemgetter
A = [[10, 8], [90, 2], [45, 6]]
print("Sorted List A based on index 0: % s" % (sorted(A, key=itemgetter(0))))
B = [[50, 'Yes'], [20, 'No'], [100, 'Maybe']]
print("Sorted List B based on index 1: % s" % (sorted(B, key=itemgetter(1))))

Output:

Sorted List A based on index 0: [[10, 8], [45, 6], [90, 2]]
Sorted List B based on index 1: [[100, 'Maybe'], [20, 'No'], [50, 'Yes']]

To sort a list of lists in descending order, the reverse parameter is used along with the key parameter, and the list name in the sorted() function.

For example,

from operator import itemgetter
C = [[10, 8, 'Cat'], [90, 2, 'Dog'], [45, 6, 'Bird']]
print("Reversed sorted List C based on index 1: % s" % (sorted(C, key=itemgetter(1), reverse=True)))

Output:

Reverse sorted List C based on index 1: [[10, 8, 'Cat'], [45, 6, 'Bird'], [90, 2, 'Dog']]

Use the lambda Expression Along With the sorted() Function to Sort a List of Lists in Python

The lambda expressions are simple one-line functions in Python.

The function sorted(List_name, key=lambda x:x[index_number]) is used to sort a list of lists by the element positioned at the specified index_number of each inner list.

For example,

A = [[100, 'Yes'], [40, 'Maybe'], [60, 'No']]
print("Sorted List A based on index 0: % s" % (sorted(A, key=lambda x:x[0])))
B = [[2, 'Dog'], [0, 'Bird'], [7, 'Cat']]
print("Sorted List A based on index 1: % s" % (sorted(B, key=lambda x:x[1])))

Output:

Sorted List A based on index 0: [[40, 'Maybe'], [60, 'No'], [100, 'Yes']]
Sorted List B based on index 1: [[0, 'Bird'], [7, 'Cat'], [2, 'Dog']]

Similarly, we can use the reverse parameter to get the output in descending order.

For example,

C = [[60, 5], [90, 7], [30, 10]]
print("Reversed sorted List C based on index 0: % s" % (sorted(C, key=lambda x:x[0], reverse=True)))

Output:

Reversed sorted List C based on index 0: [[90, 7], [60, 5], [30, 10]]

Use the sort() Function to Sort a List of Lists in Python

The sort() method sorts the list of lists in Python according to the first element of each inner list. This method makes changes in the original list itself. We use the reverse parameter to sort in descending order.

For example,

A = [[55, 90], [45, 89], [90, 70]]
A.sort()
print("New sorted list A is % s" % (A))
A.sort(reverse=True)
print("New reverse sorted list A is % s" % (A))

Output:

New sorted list A is [[45, 89], [55, 90], [90, 70]]
New reverse sorted list A is [[90, 70], [55, 90], [45, 89]]

To sort the given list according to the length of the inner lists, key=len parameter is used.

For example,

A = [[5, 90, 'Hi', 66], [80, 99], [56, 32, 80]]
A.sort(key=len)
print("New sorted list A is % s" % (A))

Output:

New sorted list A is [[80, 99], [56, 32, 80], [5, 90, 'Hi', 66]]
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python List

  • Convert a Map Object Into a List in Python
  • Read CSV to List in Python