Python Sort List

Python Sort List

3 mins read617 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 13, 2023 19:01 IST

Python sort function is a built-in function that is used to sort the objects of a list in ascending, descending or user-defined order. In this article, we will discuss how to use sort function in python.

2022_11_MicrosoftTeams-image-80-1.jpg

Let’s assume, you randomly collect thousands of data. Now you want to sort them in ascending or descending order or by the length of the values. In Python language, it’s quite easy to sort the data using the built-in function sort (). This article will briefly discuss how to use the Python sort () function with the help of examples.

Table of Content

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
β‚Ή3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks
– / –
3 months

What is the Python sort () function?

Python sort () function is a built-in function used to sort the objects of a list in ascending, descending, or user-defined order.

  • By default, the sort () function sorts the list in ascending order.
  • sort () is a method of list class, i.e., it can only be used with lists.
  • sort () function returns none and modifies the value in place.
  • Time Complexity: O(n*log(n)).

Must Read: Lists in Python

Must Read: Python Sorted () Function

Syntax

 
name_list.sort (reverse = ……, key = ………)
Copy code

Parameters

reverse: 

  • It is an optional parameter
  • If reverse = True, the list will be reversed, i.e., values will be arranged in descending order.
  • If reverse = False, then the list value will be in ascending order.
    • By default, reverse = False.

key:

  • It is also an optional parameter.
  • It takes functions or methods specifying any detailed sorting criteria.
What is Programming What is Python
What is Data Science What is Machine Learning

How to use sort () function

Sort the list in Ascending Order

Example – 1 (i)

 
#sort the list of values in ascending order
#define a list
my_list_1 = [50, 87, 37, 85, 9, 25, 45, 100, 98, 67]
#sort in ascending order
my_list_1.sort()
#call the modified list
my_list_1
Copy code

Output

Example -1 (ii)

 
#sort the list of values in ascending order (alphabetical order)
#define a list
my_list_2 = ['python', 'is', 'the', 'best', 'programming', 'language']
#sort in ascending order
my_list_2.sort()
#call the modified list
my_list_2
Copy code
#sort the list of values in descending order
#define a list
my_list_3 = [50, 87, 37, 85, 9, 25, 45, 100, 98, 67]
#sort in ascending order
my_list_3.sort(reverse = True)
#call the modified list
my_list_3
Copy code

Output

Example-2 (ii)

 
#sort the list of values in descending order (alphabatical order)
#define a list
my_list_4 = ['python', 'is', 'the', 'best', 'programming', 'language']
#sort in ascending order
my_list_4.sort(reverse = True)
#call the modified list
my_list_4
Copy code

Output

Sort list of dictionaries

Firstly, we will define a dictionary that we will use further for our examples.

 
#define a dictionary
Emp = [
{'Name' : 'Jim', 'Age' : 26, 'Job' : 'developer'},
{'Name' : 'Sam', 'Age' : 30, 'Job' : 'data analyst'},
{'Name' : 'Dean', 'Age' : 29, 'Job' : 'data scientist'},
{'Name' : 'Leo', 'Age' : 25, 'Job' : 'python developer'}
]
Copy code
Top 110+ Python Interview Questions and Answers
Top 9 NumPy Interview Questions

Example 3 (i): Sort the name of employees in descending order

 
#define custom functions to get the data from Emp
def name(Emp):
return Emp.get('Name')
#sort the name in descending order
Emp.sort(reverse = True, key = name)
Emp
Copy code

Output

Example – 3 (ii): Sort the age of Employees in Ascending Order

 
#define custom functions to get the data from Emp
def age(Emp):
return Emp.get('Age')
#sort the age in Ascending order
Emp.sort(reverse = False, key = age)
Emp
Copy code

Output

Difference Between sort () and sorted () functions in Python

sorted ()

  • Works on both sequence and collection.
  • Creates a sorted copy of the Python object and returns a sorted list of the iterable passed
  • Syntax: sorted (iterable, key, reverse = True)

sort ()

  • Works only on the list
  • Sorts the original sequence and returns none.
  • Syntax: (reverse = False, key)

Example of sort () and sorted ()

 
#use sorted () function
#define a list: employee
employee = ['Atul', 'Rashmi', 'Jaya', 'Aquib', 'Vikram', 'Chanchal', 'Mohita', 'Anshuman']
#use sorted() function
employee_sorted = sorted(employee)
#Print the sorted list
print(employee_sorted)
#print the original list
print(employee)
Copy code

Output

 
#use sort function
#define a list: employee
employee = ['Atul', 'Rashmi', 'Jaya', 'Aquib', 'Vikram', 'Chanchal', 'Mohita', 'Anshuman']
#use sort() function
employee.sort()
#print the original list
print(employee)
Copy code
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio