How To Sort List In Python?


Python provides different types of List structures in order to hold multiple elements in a single variable like an array. Generally, the same type of data is held in these elements and we may want to sort them for value or key, incremental or decremental. In this tutorial, we will examine sorting Python List structure in different ways.

Create Python List

Before starting examples sort operation examples, we will create a simple list like below. This list consists of vowels as we can see below.

mylist=['a','u','e','i']

sort() Function Syntax

sort() function has very simple syntax where parameters like reverse , key key be provided as key/value pair to pass parameter. There is no return value for the function sort() which means sorted list values will be stored in the current list.

LIST.sort(PARAMETER)
  • LIST is the list variable that contains multiple items and sorted with the sort() function.
  • PARAMETER is used to pass some arguments like reverse sort, sort according to key, etc.

Sort List with sort() Function

In this example, we will use sort() function in order to list the items of the list named mylist . After the sort operation, the elements of the list will be sorted. So we will print the list to see this.

mylist=['a','u','e','i'] 
mylist.sort() 
print(mylist)
Sort with sort() Function
Sort with sort() Function

Sort with sorted() Function

There is also a separate function named sorted() which accepts List type variables as a parameter and return as a sorted list like below. We will provide mylist and assign the sorted list as mysortedlist like below. As we can see that the difference is that the function sort() will sort in-place but the function sorted() will sort given list and return the sorted list as a return value.

mylist=['a','u','e','i'] 
sortedlist=sorted(mylist) 
print(sortedlist)
Sort with sorted() Function
Sort with sorted() Function

Sort Reverse Order with sorted() Function

We have also the ability to sort in reverse order. We will use the parameter reverse with the value true. The reverse parameter will make the sort operation in reverse which means descending.

mylist=['a','u','e','i']               
sortedlist=sorted(mylist,reverse=True) 
print(sortedlist)
#Output will be ['u', 'i', 'e', 'a']


mylist=[3,9,5,7,2,0,4,1,2]
sortedlist=sorted(mylist,reverse=True) 
print(sortedlist)
#Output will be [9, 7, 5, 4, 3, 2, 2, 1, 0]
Sort Reverse Order with sorted() Function

Sort Reverse Order with sort() Function

Function sort() also provides the ability to reverse sort. We will use the parameter reverse with the value True in order to sort in a reverse or descending way. Below we will sort some characters and numbers in reverse order.

mylist=['a','u','e','i']               
mylist.sort(reverse=True) 
print(mylist)
#Output will be ['u', 'i', 'e', 'a']

mylist=[3,9,5,7,2,0,4,1,2]               
mylist.sort(reverse=True) 
print(mylist)
#Output will be [9, 7, 5, 4, 3, 2, 2, 1, 0]
Sort Reverse Order with sort() Function

Sort Tuples with Function sorted()

Function sorted() can be used to sort List and List types like Tuples. Tuples are named as list type structure and can be sorted with the function sorted(). Below we will create a tuple named mytuple and sort it with the sorted() function.

mytuple=('a','u','e','i')
mysortedtuple=tuple(sorted(mytuple))
print(mysortedtuple)
#Output will be ('a', 'e', 'i', 'u')


mytuple=(3,9,5,7,2,0,4,1,2)
mysortedtuple=tuple(sorted(mytuple))
print(mysortedtuple)
#Output will be (0, 1, 2, 2, 3, 4, 5, 7, 9)

Sort Tuples with Function sorted()

LEARN MORE  How To Substring In Python?

Leave a Comment