# [Introduction to Python] How to sort the contents of a list efficiently with list sort

• This article is posted with the intention that the Prosta editorial department will publish a very popular Python grammar article, which will be useful for everyone's learning.

# [Introduction to Python] How to sort the contents of a list efficiently with list sort

When programming, there are occasions when you want to sort the contents of a list. Depending on the language, there is a method of realizing an existing sorting algorithm by making full use of for statements etc., which is surprisingly difficult. However, Python has a function for sorting the list, so it's easy to sort.

This time, I will explain how to sort the contents of the list.

table of contents 1 [Sort using sort function](## Sort using sort function) 2 [Reverse sort using reverse function](## Reverse sort using reverse function) 3 [Sort using sorted function](## Sort using sorted function) 3.1 [Sort in ascending order with sorted function](Sort in ascending order with ### sorted function) 3.2 [Sort in descending order with sorted function](Sort in descending order with ### sorted function) 4 [Sort multidimensional list](## Sort multidimensional list)

## Sort using the sort function

The easiest way to list in Python is to use the sort function. The syntax of the sort function is as follows.

``````list.sort()
``````

Use sort () to sort the contents of the list in ascending order. Character strings are sorted in character code order, and numerical values are sorted in ascending order. If you sort by sort (), the contents of the original list will be replaced.

``````list1 = ['python', 'list', 'sort']
print('Before sorting:{}'.format(list1))

list1.sort()
print('After sorting:{}'.format(list1))
``````

Execution result

Before sorting: [‘python’, ‘list’, ‘sort’] After sorting: ['list', ‘python’, ‘sort’]

## Reverse sort using the reverse function

You can easily sort the list using the sort function, but the order is always ascending. If you want to sort in descending order, use the reverse function. The syntax of the reverse function is as follows.

``````list.reverse()
``````

If you use reverse (), everything in the list will be reversed. Therefore, you can sort the list in descending order by using reverse () after sorting in ascending order using sort ().

``````list1 = [1,5,3,9,6,7,8]
print('Before sorting:{}'.format(list1))

list1.sort()
print('After sorting(ascending order)：{}'.format(list1))

list1.reverse()
print('After sorting(descending order)：{}'.format(list1))
``````

Execution result

Before sorting: [1, 5, 3, 9, 6, 7, 8] After sorting (ascending order): [1, 3, 5, 6, 7, 8, 9] After sorting (descending order): [9, 8, 7, 6, 5, 3, 1]

## Sorting using the sorted function

We found that the sort function can be used to sort the list in ascending order, and the reverse function can be used to sort in descending order. However, in the case of sorting using these two functions, the contents of the original list will be rewritten.

If you have sorted but want to restore it, you may want to create a new sorted list without changing the contents if possible. In such a case, the sorted function is convenient.

### Sort in ascending order with the sorted function

The sorted function is a function that sorts a list in the same way as the sort function. However, the sorted function returns a sorted list, so unlike the sort function, the contents of the original sort remain the same. The syntax for sorting in ascending order with the sorted function is as follows.

``````Listing 2= sorted(Listing 1)
``````

Passing List 1 before sorting to sorted returns the sorted list. Substituting it into Listing 2 creates a new Listing 2 that sorts Listing 1. The original list doesn't change, so it's nice to keep the original list in case something goes wrong.

``````list1 = [1,5,3,9,6,7,8]
list2 = sorted(list1)

print('Before sorting:{}'.format(list1))
print('After sorting:{}'.format(list2))
``````

Execution result

Before sorting: [1, 5, 3, 9, 6, 7, 8] After sorting: [1, 3, 5, 6, 7, 8, 9]

## Sort in descending order with the sorted function

Sorting with the sorted function results in ascending order, but of course you can also sort in descending order.

``````Listing 2= sorted(Listing 1)
``````

When using the sorted function, you can sort in descending order by setting the option called reverse to True.

``````list1 = [1,5,3,9,6,7,8]
list2 = sorted(list1, reverse=True)  #reverse to True

print('Before sorting:{}'.format(list1))
print('After sorting:{}'.format(list2))
``````

Execution result

Before sorting: [1, 5, 3, 9, 6, 7, 8] After sorting: [9, 8, 7, 6, 5, 3, 1] Multidimensional list sorting

So far we have explained how to sort a one-dimensional list, but sometimes you may want to sort a two-dimensional or larger multidimensional list. Multidimensional arrays can be sorted by using sort (), reverse (), sorted () as in 1D.

However, in the case of multidimensional, the result depends on which value is used for sorting. For example, suppose you sort the following list.

``````list1 = [[1,5,3], [6,4,8], [9,11,2]]
``````

This list1 is a two-dimensional array of three lists with three elements. The sort result of this list1 changes depending on which element of each element list is used as the key.

``````[[1, 5, 3], [6 ,4 ,8], [9, 11, 2]]  #Key the first element of each list
[[6, 4, 8], [1, 5, 3], [9, 11, 2]]  #Key the second element of each list
[[9, 11, 2], [1, 5, 3], [6, 4, 8]]  #Key the third element of each list
``````

If you sort a multidimensional list normally, it will be sorted using the first element as a key. Use itemgetter to sort by key elements.

``````from operator import itemgetter

Listing 1.sort(key=itemgetter(1)) #Listing 1は2次元以の配列
``````

itemgetter is a function of the operator library, so you need to import it first. The argument of itemgetter () represents the number of the element you want to key. In the case of itemgetter (1), the list is sorted using the first element as a key.

You can use itemgetter to choose which element to use as a key.

``````from operator import itemgetter

list1 = [[1,5,3], [6,4,8], [9,11,2]]
print('Before sorting:{}'.format(list1))

list1.sort(key=itemgetter(0))
print('After sorting(0th element)：{}'.format(list1))

list1.sort(key=itemgetter(1))
print('After sorting(First element)：{}'.format(list1))

list1.sort(key=itemgetter(2))
print('After sorting(Second element)：{}'.format(list1))
``````

Execution result

Before sorting: [[1, 5, 3], [6, 4, 8], [9, 11, 2]] After sorting (0th element): [[1, 5, 3], [6, 4, 8], [9, 11, 2]] After sorting (first element): [[6, 4, 8], [1, 5, 3], [9, 11, 2]] After sorting (second element): [[9, 11, 2], [1, 5, 3], [6, 4, 8]]