×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python List sort() Method (with Examples)

By IncludeHelp Last updated : June 20, 2023

Python List sort() Method

The sort() is an inbuilt method of the list class that is used to sort the list elements in ascending and descending order, the method is called with this list (whose elements are to be sorted) and accepts some optional parameters (explained below under parameters), the method does not return any value, it sorts this list.

Syntax

The following is the syntax of sort() method:

List_name.sort(reverse=True|False, key=function)

Parameter(s):

The following are the parameter(s):

  • reverse=True|False – It is an optional parameter, it's default value is False which sorts list elements in ascending order, if we set True, it sorts the list elements in descending order.
  • key=function – It is also an optional parameter, it can be used to specify the sorting criteria.

Return Value

The return type of this method is <class 'NoneType'>, it returns nothing.

Example 1: Sorting list without specifying any parameter

# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements
cars.sort() # sorts in ascending order

# printing the list
print("cars after sort operation...")
print("cars: ", cars)

Output

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars after sort operation...
cars:  ['Audi', 'Audi', 'BMW', 'Lexus', 'Porsche']

Example 2: Sorting list with specifying the first parameter

# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements specifying reverse=True
cars.sort(reverse=True) 

# printing the list
print("cars list elements in descending order...")
print("cars: ", cars)

# sorting the elements specifying reverse=False
cars.sort(reverse=False) 

# printing the list
print("cars list elements in ascending order...")
print("cars: ", cars)

Output

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars list elements in descending order...
cars:  ['Porsche', 'Lexus', 'BMW', 'Audi', 'Audi']
cars list elements in ascending order...
cars:  ['Audi', 'Audi', 'BMW', 'Lexus', 'Porsche']

Example 3: Sorting list with specifying the both parameters

# defining a function that will return length
def getLen(e):
    return len(e)
    
# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements specifying reverse=True
cars.sort(reverse=True,key=getLen) 

# printing the list
print("cars list elements in descending order based on length...")
print("cars: ", cars)

# sorting the elements specifying reverse=False
cars.sort(reverse=False,key=getLen) 

# printing the list
print("cars list elements in ascending order based on length...")
print("cars: ", cars)

Output

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars list elements in descending order based on length...
cars:  ['Porsche', 'Lexus', 'Audi', 'Audi', 'BMW']
cars list elements in ascending order based on length...
cars:  ['BMW', 'Audi', 'Audi', 'Lexus', 'Porsche']

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.