Updated April 13, 2023
Introduction to Python Sort List
In this article, we are discussing one of the methods of lists. In Python, the list is a data structure that contains a number of elements or an ordered sequence of elements. The elements inside this list are known as items also. In this article, we are discussing a sort() method. The sort() method is defined as sorting of the elements that are given in the list in a specified order, such as either ascending or descending order. This method can sort the list of items in ascending order by default. When we use this method, the original list will not be changed; after applying this method, you get a different sorted list.
Working of the sort() List in Python
This article will see one of the most commonly used methods of list data structure that is sort(). This method is used for arranging the list of given or specified elements in the list in either ascending or descending order.
Syntax:
list.sort([func])
Or
List.sort (reverse = True or False, key = func_name)
In the above syntax, we can see the first one is very simple, and if it just specified as sort(), then it will by default sort the elements in ascending order and the “func” parameter, which is used to specify any of the sorting criteria’s.
The second syntax can also be used, which works similar to the first one, and if we want the list to be sorted in descending order, then we have to set the reverse parameter as “True” else set it “false”, and the key parameter is used for a function that can specify sorting criteria(s).
Examples of Python Sort List
Following are the examples of python sort list
Example #1
Code:
num = [1, 3, 4, 2]
print("The given list to sort is as follows:", num)
num.sort()
print("The above list is sorted as follows:",num)
Output:
In the above program, we can see that we have sorted the simple list. As we can see, the above program has only the sort() method, So by default, the list is sorted in ascending order.
Now let us see another example in which we can sort in descending order.
Example #2
Code:
num = [13, 3, 41, 21]
print("The given list for sorting is as follows:", num)
num.sort(reverse = True)
print("The above list is sorted in descending order is as follows:",num)
Output:
In the above program, we have declared the sort() method for the given list, and it is named as “num”. This list is sorted in descending order using a parameter “reverse”, which is set to “True” so that the list can be sorted in reverse order or descending order.
Now let us see how to sort the list according to the length of the letters of words, and we can sort in either of the ways, such as ascending or descending order. In the below example, we will see how to sort the length of the values of the list in descending order. To sort in ascending order, we have to specify the reverse parameter as false or just specify it as sort() method.
Example #3
Code:
def Func(e):
return len(e)
fruits = ['Mango','Watermelon', 'Banana', 'Kiwi']
print("The list of fruits are as follows:",fruits)
fruits.sort(reverse=True, key=Func)
print("The list of fruits will be sorted according to the length of the letters in descending order:")
print(fruits)
Output:
In the above program, we can see we have declared a list of fruits which are having different lengths of words in the list. This list is sorted according to the length of the letters in each word in reverse order or descending order which we can see in the above screenshot.
As we saw in the above syntax, we have seen the parameter “key”, in which we can use for custom sorting, which can transform each element before comparisons. The function key takes in 1 value and return one value, where this returned value is used for comparisons within the sort method. Let us take the above example of the list of the fruits, which we sorted according to the length of the letters in descending order. This can also be done using a “key” function. In this below example, we will sort the length of the letters of the words in the list from shortest to longest the sort () method calls the len()method for each word to get the length value and then we can sort the list of fruits according to the length of the values of letters in words in the list given. In this, we have a key function as len() function; as we can see in the above program, we have specified key as func, which is the function name that is first defined and created.
Now let us see how to sort the list of lists in Python for a specific column or index, then we can use lambda. Let us the example below:
Example #4
Code:
emplyoee_list =[['Ashok','Patil',21],['Seema','Gali',48],['Kulwinder','Singh',52],['Lata','Agrwal',25], ['Pooja','Shetty',35]]
print("The given list of lists is as follows :")
print(emplyoee_list)
print("\n")
print ("Sorting the list of lists using lambda is as follows:")
emplyoee_list.sort(key = lambda i: i[2])
print(emplyoee_list)
Output:
Conclusion
In this article, we saw how to sort the list of elements using the sort() method in Python. In this article, we saw how we had sorted the list in either ascending or descending order using the “reverse” parameter with values “True” or “False”. To sort the list, we can just use the function sort() for sorting in ascending as it is the default if not specified; else to we can specify the reverse parameter value as “false”. If we want to sort in descending order, then we can use the reverse parameter value as “True”. We also saw how to sort a list of lists using the lambda method.
Recommended Articles
We hope that this EDUCBA information on “Python Sort List” was beneficial to you. You can view EDUCBA’s recommended articles for more information.