×

Python List sort() method

The list.sort () method in Python sorts the items of the list in place.

Syntax

list.sort(key=None, reverse=False)

Parameter

reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’ Boolean value sorting is done in ascending order.

key: This parameter represents a function to specify the sorting criteria(s)

Example 1

# Python program explaining
# the list.sort() method
# passing the alphabets list
alphaList = ['b', 'a', 'e', 'd', 'c']
print("Actual list: ",alphaList)
# sort the alphabets list in the ascending order 
alphaList.sort()
# priningt the list in a sorted order
print('Sorted list:',alphaList) 

Output

List:  ['b', 'a', 'e', 'd', 'c']
Sorted list: ['a', 'b', 'c', 'd', 'e'] 

Example 2

# Python program explaining
# the list.sort() method
weekList = ['Sun', 'Thurs', 'Mon', 'Fri', 'Tues', 'Wed']
print("Actual week list: ",weekList) 
# Sorting the week list in the descending order
weekList.sort(reverse=True)
# printing the sorted week list
print("Sorted week list:",weekList) 

Output

Actual week list:  ['Sun', 'Thurs', 'Mon', 'Fri', 'Tues', 'Wed']
Sorted week list: ['Wed', 'Tues', 'Thurs', 'Sun', 'Mon', 'Fri'] 

Example 3

# Python program explaining
# the list.sort() method
# returning the second element of the parameter 
def sortArray(arr): 
     return arr[1]   
# demonstrating the use of sorting by using using the second key  
valList = [(11, 12), (13, 13), (11, 11)] 
# sorting the array in ascending
valList.sort(key = sortArray, reverse = False)
# printing the sorted array
print("Sorted array in ascending order: \n",valList) 
# sorting the array in descending  
valList.sort(key = sortArray, reverse = True) 
# printing the sorted array
print("Sorted array in descending order: \n",valList)  

Output

Sorted array in ascending order: 
 [(11, 11), (11, 12), (13, 13)]
 Sorted array in descending order: 
 [(13, 13), (11, 12), (11, 11)] 


Related Topics

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Magento 2 Site optimization

Magento 2 Site optimization Magento 2 is a CMS, commonly referred to as Intensive efficiency. Improving the speed and reliability of your Magento 2 app helps clients to achieve a better user experience while...

2 minutes read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Python Project Ideas for Advanced Developers

Best Python Project Ideas for Advanced Developers Python is an object-oriented, high-level, interpreted programming language created by a developer known Guido Van Rossum. Python is one of the languages that gathered...

9 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.