In this Python post, we’ll learn how to use and apply Python list sort method to our Python lists. We’ll be using multiple examples to practically understand how sort method works.
After reading this post, I’m sure you will be able to easily use Python list sort in your own Python code.
So what are both waiting for? Let’s just get right into its implementation.
- What is Python List Sort Method?
- Syntax of Sort Method
- Implementing Python List Sort Method (Multiple Easy Examples)
- Example 1: Sorting List in Ascending Order
- Example 2: Sort Method Returns None
- Examples 3: Use Sorted Function to Return Sorted List
- Example 4: Sort List In Descending Order
- Example 5: Sort a List of Vowels
What is Python List Sort Method?
It is used to sort/rearrange the items of Python list in ascending or descending order. Let’s now understand the working of sort method by using practical Python code examples. But first, let’s understand the syntax of Python list sort method.
Syntax of Sort Method
The sort method has two optional parameters, means it’ll also work just fine if we don’t use them.
- Key parameter can be used as a key for the sort comparison.
- Reverse parameter is of a Boolean(True/False) type. Its used to specify whether the sorted list should be in ascending or descending order.
Implementing Python List Sort Method (Multiple Easy Examples)
Let’s now look at multiple examples on how to properly use the sort method of Python list.
Example 1: Sorting List in Ascending Order
itemsList= [7,4,5,3,1,6,9,8,3,2,10] itemsList.sort() // list will be sorted using this method print(itemsList) // [1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10]
First, have created a Python list of integers. Then we’ve applied the sort method on it and as a result, we have items of the listed sorted in ascending order.
We’ve used a duplicate item 3 as well and we can see that its also in proper sorted order.
Example 2: Sort Method Returns None
The sort method does not return the sorted list. It just updates the current list. See below code:
itemsList= [1,6,9,8,7,3,2,10,4,5,3] sortedList=itemsList.sort() print(sortedList) // None
We’ve created a new variable to store whatever value the sort method will return and as you can see, it returns none.
Examples 3: Use Sorted Function to Return Sorted List
If you want a new list to be returned which is sorted then use this Python built-in sorted function. It works just like the sort method but the difference is that it does not update the current Python list, it just return a new sorted list. While the sort method update the current Python list. See below code on how to use the sorted function:
itemsList= [1,6,9,8,7,3,2,10,4,5,3] sortedList=sorted(itemsList) print(itemsList) // [1, 6, 9, 8, 7, 3, 2, 10, 4, 5, 3] print(sortedList) // [1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10]
You can see that the current list is not sorted. The sorted function returned a new sorted list which we have stored in a new variable and then we have printed it.
We can use its reverse parameter to make the list sorted in descending order. By default, it is in ascending order. This parameter takes a Boolean(True, False) value. See below code:
itemsList= [1,6,9,8,7,3,2,10,4,5,3] sortedList=sorted(itemsList,reverse=True) print(sortedList) // [10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1]
Example 4: Sort List In Descending Order
The sort method also has a parameter called reverse which takes a Boolean(True, False). By default, it is false which sorts the list in ascending order. Let’s make it true and see what result we’ll get after that. See below code:
itemsList= [1,6,9,8,7,3,2,10,4,5] itemsList.sort(reverse=True) print(itemsList) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
We now have a sorted Python list in descending order.
Example 5: Sort a List of Vowels
itemsList= ['e','i','a','u','o'] itemsList.sort() print(itemsList) // ['a', 'e', 'i', 'o', 'u']
The Python list sort method sorts this list in a proper alphabetical order. We can change its order to descending by setting the reverse parameter to true. See below code:
itemsList= ['e','i','a','u','o'] itemsList.sort(reverse=True) print(itemsList) // ['u', 'o', 'i', 'e', 'a']
So this is how you can easily use the Python list sort method. We also have practically discussed how to use the Python’s built-in sorted function.
Feel free to ask if you still have any questions related to the implementation of Python list sort method. I’d be glad to answer your questions.
To conclude, we’ve discussed how to use and customize Python list sort method. Also, we have practically implemented the Python’s built-in sorted function. Click here if you want to learn more about the sort method or the sorted function. Your feedback would be much appreciated. Thank you for reading this post.