In this article, we will discuss about the usage details of
sort() method of Python list.
Table Of Contents
sort() method of List provides us the facility to sort the contents of the list, either in ascending or descending order. It can also accept a key function as an optional argument. If provided, then this key function will be called upon each element of list, and the returned values will be used for comparision. while sorting the elements in list.
Syntax of List sort() method
It accepts 2 optional parameters
key: An optional parameter
It can be a global function or Lambda function. If this
keyfunction is provided then while sorting, this key function will be called on each element, and the returned values will used for comparision, while comparing the elements.
reverse: An optional parameter
By default its value is
False. If it
Truevalue is provided for this parameter, then the sort() function will sort the list elements in decreasing order.
It does not return anything. Basically the return value is
None. But it modifies the content of the calling list object.
Let’s see some examples.
Example 1: Using List sort() without any parameter
Suppose we have a list of numbers and we want to sort the numbers in ascending order. For that, we will just call the
sort() function on the list object and it should sort the list elements in ascending order
listOfNumbers = [23, 45, 1, 67, 21, 56, 10] # Sort list elements in ascending order listOfNumbers.sort() print(listOfNumbers)
[1, 10, 21, 23, 45, 56, 67]
As guessed, it modified the list content and sorted all the elements in ascending order. But what if we want to sort the elements in descending order? For that you need to look into our next example.
Example 2: Using List sort() with reverse parameter
Suppose we have a list of number and we want to sort this numbers in descending order. For that we will call the
sort() function of list and pass a parameter
reverse with value
True. It will modify the list by sorting the list elements in decreasing order.
listOfNumbers = [23, 45, 1, 67, 21, 56, 10] # Sort list elements in descending order listOfNumbers.sort(reverse=True) print(listOfNumbers)
[67, 56, 45, 23, 21, 10, 1]
As we guessed, it sorted the list elements in decreasing order.
Important point to remember
The sort() function always modified the calling list object.
Example 3: Using List sort() with key parameter
The sort() function uses less than operator to compare the elements while sorting. If we provide a function as key parameter in the sort function then it will call the key function on each element of list, and will compare the returned values while sorting. So, basically instead of comparing elements directly, it will compare the values returned by the key function for each element.
For example we have a list of strings. And if we will call the sort() function on that list then it will sort the strings in list in lexical order. But we do not want that we want to sort the strings in list by their length. It means the string with shortest length should be at the beginning of the list and a string with the largest size should be at the end of the list. So basically, we want to sort a list of strings based on the size of the string elements.
For that we will call the sort function of list and passing parameter key in it as aparameter. The value of the key will be a Lambda function, which accepts string is an argument and it returns the length of the string.
So while sorting the sort function we will call the key function for each element of the list and whatever value the key function returns it will use that for comparison. As we provide a lambda function that returns the length of the string, so the list will be sorted based on the length of the strings in it. Let us see the example
listOfStrings = ["is", "at", "what", "how", "the"] # Sort strings in list by the size listOfStrings.sort(key = lambda elem: len(elem)) print(listOfStrings)
['is', 'at', 'how', 'the', 'what']
As you guessed the list got sorted but by the size of the string elements. We can also use the reverse parameter along with the key parameter.
Like, in previous example we sorted the list of strings in ascending order of the size. If you pass the reverse flag as true then it will sort the list of strings by their size but in decreasing order.
# Sort strings in list by the size in decreasing order listOfStrings.sort(key = lambda elem: len(elem), reverse=True)
Similarly we can also the a normal function as key function instead of lambda function. For example,
listOfStrings = ["is", "at", "what", "how", "the"] # Sort strings in list by the size listOfStrings.sort(key = len) print(listOfStrings)
['is', 'at', 'how', 'the', 'what']
Here, we passed the len() function as key function. It sorted the strings in list by the size.
We learned all about the sort() method of Python List. Thanks.
Thanks for reading.