Introduction

Sorting a list is one of the most common operations that you might encounter while working with Python. In this tutorial, we will explore different approaches to sort a list in Python. we will check how to sort lists using the sort method and the sorted method. This tutorial is suitable for beginners who are just starting with Python, as well as for experienced developers who want to sharpen their skills in sorting data using Python.

Table of Contents :

  • Sorting a list in Python
    • Sorting a list using sort() method
    • Sorting list of Tuples
    • Sorting List in Python using sorted function 

Sorting a list in Python :

  • We can sort a list in Python by using the :
    • sort() method
    • sorted() method

Sorting a list using sort() method :

  • The basic syntax of  sort  method is :  list_name.sort() 
  • The  sort  method changes the order of the elements within the original list.
  • In other words the original list object remains the same but the order of the elements within the list changes.
  • This type of sorting is called sorting in place.
  • The  sort  method sorts the list of integers in ascending order i.e. smallest to largest.
  • To sort the list in descending order we need to pass an argument  reverse=True  in the  sort  method.
  • The  sort  method sorts the list of strings alphabetically.
  • To sort the list in reverse alphabetical order we need to pass an argument  reverse=True  in the  sort  method.
  • Code Sample : 

my_list = [3, 4, 1, 2]
print("my_list = ", my_list)

# Output
# my_list =  [3, 4, 1, 2]



# Sorting the list
my_list.sort()
print("my_list = ", my_list)

# Output
# my_list =  [1, 2, 3, 4]



# Sorting the list in reverse order
my_list.sort(reverse=True)
print("my_list = ", my_list)

# Output
# my_list =  [4, 3, 2, 1]



my_str_list = ["lmno", "abc", "fgh", "xyz"]

# Sorting the list
my_str_list.sort()
print("my_str_list = ", my_str_list)

# Output
# my_str_list =  ['abc', 'fgh', 'lmno', 'xyz']



# Sorting the list in reverse order
my_str_list.sort(reverse=True)
print("my_str_list = ", my_str_list)

# Output
# my_str_list =  ['xyz', 'lmno', 'fgh', 'abc']


Sorting list of Tuples : 

  • The sort method can be used to sort a list of tuples.
  • In this case we need to pass a function as an argument to the  sort  method.
  • The main purpose of this function is to return the key within the tuple which will decide the sorting.
  • In the code sample below we make use of an anonymous function to return the key which will be used for sorting purpose.
  • Code Sample : 

tup = [('John', 100), ('alice', 15), ('Steve', 200), ('Dave', 5)]
print("Tuple before sorting : ",tup)

tup.sort(key=lambda x: x[1])
print("Tuple after sorting : ", tup)

# Output
# Tuple before sorting :  [('John', 100), ('alice', 15), ('Steve', 200), ('Dave', 5)]
# Tuple after sorting :  [('Dave', 5), ('alice', 15), ('John', 100), ('Steve', 200)]


Sorting List in Python using sorted function :

  • As we have just read that  sort()  method sorts the elements of the list in the original list itself.
  • Unlike  sort()  method,  sorted()  method returns a new list after sorting the original list.
  • The original list is not modified by the  sorted()  method.
  • The  sorted()  method sorts the list of integers in ascending order i.e. smallest to largest.
  • To sort the list in descending order we need to pass an argument  reverse=True  in the  sorted()  method.
  • The  sorted()  method sorts the list of strings alphabetically.
  • To sort the list in  reverse alphabetical order we need to pass an argument  reverse=True  in the  sorted()  method.
  • Code Sample : 

my_list = [3, 4, 1, 2]
print("my_list = ", my_list)

# Output
# my_list =  [3, 4, 1, 2]



# Sorting the list
new_list = sorted(my_list)
print("my_list = ", my_list)
print("new_list = ", new_list)

# Output
# my_list =  [3, 4, 1, 2]
# new_list =  [1, 2, 3, 4]



# Sorting the list in reverse order
new_list = sorted(my_list, reverse=True)
print("my_list = ", my_list)
print("new_list = ", new_list)

# Output
# my_list =  [3, 4, 1, 2]
# new_list =  [4, 3, 2, 1]



my_str_list = ["lmno", "abc", "fgh", "xyz"]

# Sorting the list
new_str_list = sorted(my_str_list)
print("my_str_list = ", my_str_list)
print("new_str_list = ", new_str_list)

# Output
# my_str_list =  ['lmno', 'abc', 'fgh', 'xyz']
# new_str_list =  ['abc', 'fgh', 'lmno', 'xyz']



# Sorting the list in reverse order
new_str_list = sorted(my_str_list, reverse=True)
print("my_str_list = ", my_str_list)
print("new_str_list = ", new_str_list)

# Output
# my_str_list =  ['lmno', 'abc', 'fgh', 'xyz']
# new_str_list =  ['xyz', 'lmno', 'fgh', 'abc']


Prev. Tutorial : Index of an element

Next Tutorial : Slicing a list