Python 3 - List sort() Method


Advertisements

Description

The sort() method sorts objects of list, use compare function if given.

Syntax

Following is the syntax for sort() method −

list.sort([func])

Parameters

NA

Return Value

This method does not return any value; it simply sorts the contents of the given list.

Example

The following example shows the usage of sort() method.

#!/usr/bin/python3

list1 = ['physics', 'Biology', 'chemistry', 'maths']
list1.sort()
print ("list now : ", list1)

Result

When we run above program, it produces the following result −

list now :  ['Biology', 'chemistry', 'maths', 'physics']
python_lists.htm
Advertisements