Python Sort List

# Python Sort List

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.

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.

## 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: 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.

## 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_1Copy code```

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_2Copy code```

Output

### 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_3Copy code```

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_4Copy code```

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'}]Copy code`
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due to...read more
Top 9 NumPy Interview Questions
In this article, we will discuss basic and intermediate NumPy Interview Questions in most simplified ways with example.

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)EmpCopy 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 orderEmp.sort(reverse = False, key = age)EmpCopy 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 listprint(employee_sorted) #print the original listprint(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 listprint(employee)Copy code```

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.