×

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

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

Python program to add two number

Python program to add two number This program will add the two numbers and display their sum on the screen. Example: Input: Number1 = 20        Number2 = 30   Output: Sum =...

2 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.