  From this tutorial, you will be learning about . You will see how to use it with lists with the help of .

Note: The syntax used in the below section is for Python 3. You can change it to any other versions of Python.

Python List Sort

To Learn about Lists – Read Python List

List Sort Method

The sort method performs the sorting of list elements in either ascending or descending direction. Its syntax is as follows:

List_name.sort( = …, reverse = ...)

When the sort() gets called without arguments, it sorts in the ascending order by default. It doesn’t have a return value.

It merely returns to the next line without returning any output.

Please note that it is not related to the built-in sorted() function. The sort method mutates or modifies an old list whereas sorted() creates a new sorted sequence.

How does the Sort method work?

When we call this method, it traverses the list elements in a loop and rearranges them in ascending order when there are no arguments.

If we specify “reverse = true” as an argument, then the list gets sorted in the descending order.

The parameter is the steps that the method must go through when sorting through a list of elements. The value given to the key can be a function or a simple calculation etc.

The flowchart for the mechanism is as follows: Sort Method Examples

Sort a list of numbers:

a. Sorting a list of numbers in ascending order

Natural_numbers = [1,4,23,3,2,1,0,9,7]

Natural_numbers.sort()

print (Natural_numbers)

Output:

[0, 1, 1, 2, 3, 4, 7, 9, 23]

b. Sorting a list of numbers in descending order

Natural_numbers = [1,23,4,25,22,3,4,5,9,7,5]

Natural_numbers.sort(reverse = True)

print (Natural_numbers)

Output:

[25, 23, 22, 9, 7, 5, 5, 4, 4, 3, 1]

Sort a list of letters:

a. Sorting a list of letters in ascending order

Vowels = ["a", "u", "i", "o", "e"]

Vowels.sort()

print (Vowels)

Output:

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

b. Sorting a list of letters in descending order

Vowels = ["a", "u", "i", "o", "e"]

Vowels.sort(reverse = True)

print (Vowels)

Output:

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

Sort a list of strings:

a. Sorting a list of strings in ascending order

Fruits = ["Apple", "Banana", "Tomato", "Grapes"]

Fruits.sort()

print (Fruits)

Output:

['Apple', 'Banana', 'Grapes', 'Tomato']

b. Sorting a list of strings in descending order

Fruits = ["Apple", "Banana", "Tomato", "Grapes"]

Fruits.sort(reverse = True)

print (Fruits)

Output:

['Tomato', 'Grapes', 'Banana', 'Apple']

Sort list by a key function:

a. Sorting a list by a key function (ascending)

# Let's sort on the basis of 2nd element
def keyFunc(item):
return item

# Unordered list
unordered = [('b', 'b'), ('c', 'd'), ('d', 'a'), ('a', 'c')]

# Order list using key
unordered.sort(key=keyFunc)

# Output the sorted list
print('Ordered list:', unordered)

Output:

Ordered list: [('d', 'a'), ('b', 'b'), ('a', 'c'), ('c', 'd')]

b. Sorting a list using a key function (descending)

# Let's sort on the basis of 2nd element
def keyFunc(item):
return item

# Unordered list
unordered = [('b', 'b'), ('c', 'd'), ('d', 'a'), ('a', 'c')]

# Order list using key in the reverse direction
unordered.sort(key=keyFunc, reverse = True)

# Output the sorted list
print('Ordered list:', unordered)

Output:

Ordered list: [('c', 'd'), ('a', 'c'), ('b', 'b'), ('d', 'a')]

Best,

TechBeamers

SHARE