×

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 lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

Python id() function

Python id() function The id() function in Python returns an id for the specified object where all the objects in has its own unique id. Syntax id(object) Parameter object: This parameter represents any object, String, Number, List,...

1 minute read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

Rank Based Percentile GUI Calculator using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities...

3 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python File Handling

What is the file? The file is a collection of data in a single unit. It is used to store information in the non-volatile memory such as hard-disk. Computer programs generally run on the RAM...

7 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.