Javatpoint Logo
Javatpoint Logo

Python List sort() Method

Python sort() method sorts the list elements. It also sorts the elements into descending and ascending order. It takes an optional parameter reverse which sorts the list into descending order. Be defauly list sorts into ascending order. The examples and signature are given below.

Signature

Parameters

No parameter

Return

It returns None.

Let's see some examples of sort() method to understand it's functionality.

Python List sort() Method Example 1

It is a simple example which sorts two lists in ascending order. See the example below.

Output:

['a', 'p', 'p', 'l', 'e']
[6, 8, 2, 4]

After Sorting:
 ['a', 'e', 'l', 'p', 'p']
[2, 4, 6, 8]

Python List sort() Method Example 2

This example sorts the list into descending order.

Output:

[8, 6, 4, 2]

Python List sort() Method Example 3

This example sorts the char list into descending order.

Output:

['a', 'p', 'p', 'l', 'e']
[6, 8, 2, 4]

After Sorting:
 ['p', 'p', 'l', 'e', 'a']
[8, 6, 4, 2]

Next TopicPython Lists




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA