The list is a very popular data type that is provided by Python. Python list may contain different data types like string, number, integer, floating-point, etc. These items can be sorted by using the list built-in sort()
method. In this tutorial, we will learn how to sort the list, incremental sort, decremental sort, sort nested lists, etc.
sort() Function Syntax
The list sort() method has the following syntax where two parameters are optional.
LIST.sort(reverse=TRUE|FALSE , key=FUNC)
- LIST is the list that will be sorted.
- reverse is a parameter to sort the list in reverse. By default this parameter is False.
- key is a function that is used to sort given list items which will be helpful for complex lists.
Sort String List
A list may contain only string items. The sort() method can be used to sort these string items easily. The sort will be completed inside the list and there will be no return value.
mylist = [ "Ahmet" , "Ismail" , "Elif" , "Baydan" ]
print(mylist)
# ['Ahmet', 'Ismail', 'Elif', 'Baydan']
mylist.sort()
print(mylist)
# ['Ahmet', 'Ismail', 'Elif', 'Baydan']
We can also reverse sort the given string list. We wil ljust provide the reverse
parameter with the True
value like below.
mylist = [ "Ahmet" , "Ismail" , "Elif" , "Baydan" ]
print(mylist)
# ['Ahmet', 'Ismail', 'Elif', 'Baydan']
mylist.sort( reverse=True )
print(mylist)
# ['Ismail', 'Elif', 'Baydan', 'Ahmet']
Sort Number List
A list may contain only numbers and the sort() method can be used to sort these number items easily. The sort will be completed inside the list and there will be no return value and sorted list items will be stored in the list named mylist. The sort method sorts the given list incrementally.
mylist = [ 2 , 4 , 6 , 9 , 7 , 1 ]
print(mylist)
# [ 2 , 4 , 6 , 9 , 7 , 1 ]
mylist.sort()
print(mylist)
# [1, 2, 4, 6, 7, 9]
We can also reverse sort a number list by providing the reverse
parameter as True
like below. Normally list is sorted incrementally but the reverse changes it into a decremental sort.
mylist = [ 2 , 4 , 6 , 9 , 7 , 1 ]
print(mylist)
# [ 2 , 4 , 6 , 9 , 7 , 1 ]
mylist.sort(reverse=True)
print(mylist)
# mylist = [ 2 , 4 , 6 , 9 , 7 , 1 ]
print(mylist)
# [ 2 , 4 , 6 , 9 , 7 , 1 ]
mylist.sort(reverse=True)
print(mylist)
# [9, 7, 6, 4, 2, 1]
Sort Dictionary List
A list may contain multiple dictionary items and these items can be sorted according to the specified key-value pair. In order to sort a list of dictionaries, the key parameter of the sort() method will be used.
def MyFunction(e):
return e['age']
mylist = [ { "name":"ahmet" , "age":8 } , {"name":"ismail" , "age":36}, {"name":"elif" , "age":12} ]
mylist.sort(key=MyFunction)
print(mylist)
# [{'name': 'ahmet', 'age': 8}, {'name': 'elif', 'age': 12}, {'name': 'ismail', 'age': 36}]
Alternatively, the given list of dictionaries can be listed in a reverse manner. We will just provide the reverse=True
to the sort() method like below.
def MyFunction(e):
return e['age']
mylist = [ { "name":"ahmet" , "age":8 } , {"name":"ismail" , "age":36}, {"name":"elif" , "age":12} ]
mylist.sort( reverse= True , key=MyFunction )
print(mylist)
# [{'name': 'ismail', 'age': 36}, {'name': 'elif', 'age': 12}, {'name': 'ahmet', 'age': 8}]
Sort List of List (Nested List) For a Specified Column/Index/Item
A list may contain multiple lists as item. An these child lists also contain the same structured data like “name”,”age”. We can sort the same structured data lists by using sort function. In the following example we will create a function named MyFunction() which is used to return the sort item or value. Them the function name will set as the key for the sort() method.
def MyFunction(e):
return e['age']
mylist = [ [ "name":"ahmet" , "age":8 ] , ["name":"ismail" , "age":36], ["name":"elif" , "age":12] ]
mylist.sort( reverse= True , key=MyFunction )
print(mylist)
# [{'name': 'ismail', 'age': 36}, {'name': 'elif', 'age': 12}, {'name': 'ahmet', 'age': 8}]