Python List sort() MethodPython Functions

Difficulty Level:
Difficulty Level:

The sort() function is used to sort the original list in a defined order.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

list1 = ['e', 'a', 'u', 'o', 'i']
print(list1)

list1.sort(reverse=True)
print(list1)

list1.sort(reverse=False)
print(list1)

Output:

['e', 'a', 'u', 'o', 'i']
['u', 'o', 'i', 'e', 'a']
['a', 'e', 'i', 'o', 'u']

Syntax

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

Parameters

Name Description
reverse If true, the sorted list is reversed (or sorted in Descending order).
key The function that serves as a key for the sort comparison.

Return Value

It doesn’t return any value.

Advertisement

© 2021 Pythonexample.org, all rights reserved. Privacy Policy | Contact Us