×

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

AES CTR Python

Python Programming Language  Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

GUI Calendar using Tkinter 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...

3 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

Python's Qstandarditemmodel

Python More interactive and user-friendly than any other programming language is Python. Many libraries are used by the python programming language to speed up procedures. Python can be used to develop...

3 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.