Python Sort List
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.
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
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 = β¦β¦β¦)
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.
How to use sort () function
Sort the list in Ascending Order
Example β 1 (i)
#sort the list of values in ascending order
#define a listmy_list_1 = [50, 87, 37, 85, 9, 25, 45, 100, 98, 67]
#sort in ascending ordermy_list_1.sort()
#call the modified listmy_list_1
Output
Example -1 (ii)
#sort the list of values in ascending order (alphabetical order)
#define a listmy_list_2 = ['python', 'is', 'the', 'best', 'programming', 'language']
#sort in ascending ordermy_list_2.sort()
#call the modified listmy_list_2
Output
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Sort the list in descending order
Example β 2 (i):
#sort the list of values in descending order
#define a listmy_list_3 = [50, 87, 37, 85, 9, 25, 45, 100, 98, 67]
#sort in ascending ordermy_list_3.sort(reverse = True)
#call the modified listmy_list_3
Output
Example-2 (ii)
#sort the list of values in descending order (alphabatical order)
#define a listmy_list_4 = ['python', 'is', 'the', 'best', 'programming', 'language']
#sort in ascending ordermy_list_4.sort(reverse = True)
#call the modified listmy_list_4
Output
Sort list of dictionaries
Firstly, we will define a dictionary that we will use further for our examples.
#define a dictionaryEmp = [ {'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'}]
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
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 orderEmp.sort(reverse = False, key = age)Emp
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 listprint(employee_sorted)
#print the original listprint(employee)
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 listprint(employee)
Output
Conclusion
sort (reverse, key) function is used to sort the list in ascending, descending or in the user-defined order. In the last section of the article, we also discussed the difference between sort and sorted function in Python.
Hope this article, will help you to understand how to use sort function in python and how it differs from sorted function.
Top Trending Python Articles
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Python Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
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