# How to Sort a List of Lists in Python

To sort a list of lists in Python, use the sort() function. The sort() is a built-in Python function that sorts the list ascending by default.

The list is one of the exciting and most essential concepts in the Python programming language. A list is one of the fundamental data types in Python, and it is similar to arrays in C++, but a list can contain heterogeneous elements (i.e., various types of data).

We can modify the elements of a list, i.e., insert, delete, modify. That means lists are mutable. The list index starts at 0.

``````l = [1, "2", "Three"]
print(l)
print(type(l))
``````

#### Output

``````[1, '2', 'Three']
<class 'list'>``````

There are different types of lists, but the most important type which is very useful in Machine Learning is the List of Lists (or Nested List), as the name suggests, i.e., a list containing many other lists or lists of lists.

#### Example

``lOL = [ ‘0’, [‘1.1’, ‘1.2’], [‘2.1’, [ ‘2.2.1’, ‘2.2.2’]], ‘3’]``

## Sorting the elements by Column

### Sorting without using a built-in function

``````def sort_lists(list1, c, l):
print(list1)
for i in range(l-1):
print("i", i)
# if the next element is greater than the first element, swap it.
for j in range(i+1, l):
print("j", j)
if (list1[i][c] > list1[j][c]):
print("i am in if")
list1[i][c], list1[j][c] = list1[j][c], list1[i][c]
print(list1, "when j is", j)
return list1

L=[[1, 2, 3, 4],[1, 5, 6, 7],[7, 0, 3, 6]]
size = 3
c = 1 # c is the column according to which you want to sort the data

sort_lists(L, c, size)
print(L)
``````

#### Output

``````[[1, 2, 3, 4], [1, 5, 6, 7], [7, 0, 3, 6]]
i 0
j 1
j 2
i am in if
[[1, 0, 3, 4], [1, 5, 6, 7], [7, 2, 3, 6]] when j is 2
i 1
j 2
i am in if
[[1, 0, 3, 4], [1, 2, 6, 7], [7, 5, 3, 6]] when j is 2
[[1, 0, 3, 4], [1, 2, 6, 7], [7, 5, 3, 6]]``````

### Sorting using the built-in function

Using the sort() function, we can sort a list according to the first columns.

``````L = [[1, 2, 3, 4], [1, 5, 6, 7], [7, 0, 3, 6]]
L.sort()
print(L)``````

#### Output

``[[1, 2, 3, 4], [1, 5, 6, 7], [7, 0, 3, 6]]``

To get the output in descending order, use the key “reverse” in the sort() function.

``````L = [[1, 2, 3, 4], [1, 5, 6, 7], [7, 0, 3, 6]]
L.sort(reverse=True)
print(L)``````

#### Output

``[[7, 0, 3, 6], [1, 5, 6, 7], [1, 2, 3, 4]]``

### Sort the elements by any other column

To sort the elements using any other column, we can use the “key” parameter using the lambda function.

``````L = [[1, 2, 3, 4], [1, 5, 6, 7], [7, 0, 3, 6]]
L.sort(key=lambda x: x)
print(L)``````

#### Output

``[[7, 0, 3, 6], [1, 2, 3, 4], [1, 5, 6, 7]]``

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

``````L = [[6, 2, 3, 4], [5, 5, 6, 7], [7, 0, 3, 6]]
L.sort(key=lambda x: x)
print(L)``````

#### Output

``[[7, 0, 3, 6], [6, 2, 3, 4], [5, 5, 6, 7]]``

### Sorting the elements by Size

To sort the list according to the size of the nested list, we can use the key = len.

``````L = [[6, 2, 3], [5, 5], [7, 0, 3, 6]]
L.sort(key=len)
print(L)``````

#### Output

``[[7, 0, 3, 6], [6, 2, 3, 4], [5, 5, 6, 7]]``

### Sorting the elements by Sum

To sort the list according to the length of the nested list, we can use the key = sum.

``````L = [[6, 2, 3],[5, 5],[7, 0, 3, 6]]
L.sort(key=sum)
print(L)``````

#### Output

``[[5, 5], [6, 2, 3], [7, 0, 3, 6]]``

## Conclusion

To sort a list of lists in Python, use built-in functions like sort(), lambda, or user-defined functions.

That’s it for this tutorial.