Python List sort() Function Tutorial

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.

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.

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.

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 containt 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}]

Leave a Comment