How Can We Help?
Introduction:
In this tutorial, we are going to explain how to use Python list sort() Method with basic syntax and many examples for better understanding.
Python list sort() method. It sorts the elements of a list, and It modifies the list in-place, which can be compared with func, if it is given.
Syntax:
<list_variable_name>.sort(reverse=True|False, key=Func)
Input Parameters:
- list_variable_name : The lists that we want to count a particular element inside.
- Func: A function to specify the sorting criteria(s). It’s an optional argument.
- Reverse: reverse=True will sort the list descending. The Default value is reverse=False. Which means sort the list ascending. It’s an optional argument.
Return Value:
Python list sort() method doesn’t return any value (returns None).
Example
# Number List num=[10,52,5,20,0] # Sort number list num.sort() # Print updated number list print("Sorted number list: ",num)
Example output
Sorted number list: [0, 5, 10, 20, 52]
In the above example, list variable ‘num’ is sorted by ” num.sort()” and returns sorting result as ascending order of number.
In this tutorial, you have learned how to sort the list by using Python list sort() method.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
Similar Python list Methods:
- list.append(object) : It appends object to the given list.
- list.extend(seq) : It appends the contents of sequence to the list.
- list.count(object) : It returns count of how many times object occurs in the list.
- list.index(object) : It returns the lower index value of passed object in the list, if object is present in the list.
- list.insert(index,object) : It inserts object into the list at offset index.
- list.pop(obj=list[-1]) : It removes last object from the list and returns removed object of a list.
- list.remove(object) : It removes object from the list.
- list.reverse() : It reverses objects of list in place.
Python tutorials list:
- Python tutorials list : access all python tutorials.