Different Ways to sort a Python List
As the Python list is Heterogenous, i.e. it can be used to save data of various data types, python provides us with the in-built methods to sort this data. We can sort this data in different ways, like ascending order, descending order, custom sort, case-sensitive sort, case-in-sensitive sort, or reversing the order, etc. Let us go through all these methods one by one:
Ascending Order – in-place sorting
In Python, we have the sort() method to sort the list in ascending order by default. This method can be used to sort the numbers as well as alphabets. It will perform the in-place sorting, i.e. it will modify the order of the original list.
Few Examples:
In-place Sorting.
namelist = ["Charlie", "David", "Adam"]
namelist.sort()
print(namelist)
Sorting Numbers
markslist = [55, 80, 60, 90, 70]
markslist.sort()
print(markslist)
Ascending Order – temporary sort
As the sort() method will perform the in-place sorting, i.e. modify the original list, Python also provides with the sorted() function to perform the temporary sorting. It will not modify the original list, we have to save the content of the sorted() method in a new variable.
Original list is not modified.
namelist = ["Charlie", "David", "Adam"]
result = sorted(namelist)
print("Original List :", namelist)
print("Sorted List :",result)
Differences between sort() method and sorted() function
Here are the key differences between the sort() method and the sorted() function:
Descending order – using the key
In case, the list needs to be sorted in the descending order, we simply have to pass the argument “reverse = True” to sort the items of the list in descending order. This argument works for both the sort() method and the sorted() function.
sort() method: Descending order
namelist = ["Charlie", "David", "Adam"]
namelist.sort(reverse = True)
print(namelist)
sort() method: Sorting numbers in descending order
markslist = [55, 80, 60, 90, 70]
markslist.sort(reverse = True)
print(markslist)
sorted() function
namelist = ["Charlie", "David", "Adam"]
final = sorted(namelist, reverse = True)
print(final)
Custom sort and Case-insensitive Sort
We can create our custom function to return the order in which items need to be sorted. This function can be called by passing the argument “key = function name” to the sort method.
Case-sensitive sort
By default, the sort() method and the sorted() functions will sort all the capital letters first and then the lower case letters, which means by default they are performing the case-sensitive sort.
Case-insensitive sort
For the case-insensitive sort, we have to make sure that all the content shares the same case, i.e. either upper or lower. Here, in this case, we will convert all text to lowercase using the in-built function str.lower () and pass this function as the key to the sort() method or the sorted() function.
Custom sort, as per the length of characters in each word.
def itemlength(name):
return len(name)
namelist = ["Charlie", "David", "Adam", "Benjamin"]
namelist.sort(key = itemlength)
print(namelist)
Case sensitive sort
namelist = ["Charlie", "David", "adam", "Benjamin"]
namelist.sort()
print(namelist)
Ignoring the case.
namelist = ["Charlie", "David", "adam", "Benjamin"]
namelist.sort(key = str.lower)
print(namelist)
Sorting a nested list
Nested list means when we have a list inside another list. For example, we have data of 5 employees in a list with columns as Name, Gender, and Experience. Let’s see how sorting will work in this case:
Default Sort
By default, it will sort the items based on the first column in the ascending order.
employees = [["Bill", "Male", 10], ["Mark", "Male", 8], ["Lisa", "Female", 8], ["Marie", "Female", 7], ["Steve", "Male", 12]]
employees.sort()
print(employees)
Sort a Specific Column
To sort the data in a specific order, the parameter key has to be used along with the lambda function.
In the below example, we are sorting elements as per the experience.
employees = [["Bill", "Male", 10], ["Mark", "Male", 8], ["Lisa", "Female", 8], ["Marie", "Female", 7], ["Steve", "Male", 12]]
employees.sort(key = lambda n : n[2])
print(employees)
Reversing order
We can reverse the items of the list using two ways
- The reverse() method
- Negative step
Using reverse method.
namelist = ["Charlie", "David", "Adam", "Benjamin"]
namelist.reverse()
print(namelist)
Using negating step
namelist = ["Charlie", "David", "Adam", "Benjamin"]
print(namelist[::-1])