In the previous article, we have discussed Python List reverse() Method with Examples
List in Python:
Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.
List sort() Method in Python:
The sort() method sorts the elements of a list in ascending or descending order.
By default, the sort() method ascends the list.
Syntax:
list.sort(key=...., reverse=....)
Alternatively, you can use Python’s built-in sorted() function to accomplish the same thing.
sorted(list, key=...., reverse=....)
Note: The most basic distinction between sort() and sorted() is that sort() changes the list directly and returns no value, whereas sorted() does not change the list and returns the sorted list.
Parameter Values of sort():
Sort() does not require any additional parameters by default. It does, however, have two optional parameters:
- reverse: This is Optional. The list will be sorted descending if reverse=True is set. The default value is reverse=False.
- key: This is Optional. A function that allows you to specify the sorting criteria (s)
Return Value:
The sort() method produces no output. Instead, it modifies the original list.
Use sorted if you want a function to return the sorted list rather than change the original list ().
Examples:
Example1:
Input:
Given List = ['g', 'r', 'c', 'x', 'e']
Output:
The Given original list is : ['g', 'r', 'c', 'x', 'e'] The given list after sorting in ascending order: ['c', 'e', 'g', 'r', 'x']
Example2:
Input:
Given List = ['hello', 'this', 'is', 'btechgeeks']
Output:
The Given original list is : ['hello', 'this', 'is', 'btechgeeks'] The given list after sorting in descending order: ['this', 'is', 'hello', 'btechgeeks']
List sort() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
1) sort in Ascending Order
Approach:
- Give the list as static input and store it in a variable.
- Print the given original list.
- Apply sort() function to the given list to sort the given list elements in ascending order.
- Print the list after sorting all its elements in ascending order.
- The Exit of Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ['g', 'r', 'c', 'x', 'e'] # Print the given original list. print("The Given original list is :", gvn_lst) # Apply sort() function to the given list to sort the given list elements in # ascending order. gvn_lst.sort() # Print the list after sorting all its elements in ascending order. print("The given list after sorting in ascending order: ", gvn_lst)
Output:
The Given original list is : ['g', 'r', 'c', 'x', 'e'] The given list after sorting in ascending order: ['c', 'e', 'g', 'r', 'x']
2) sort in Descending Order
Approach:
- Give the list as static input and store it in a variable.
- Print the given original list.
- Apply sort() function to the given list by passing the argument as reverse = “True” to sort the given list elements in descending order.
- Print the list after sorting all its elements in ascending order.
- The Exit of Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ["hello", "this", "is", "btechgeeks"] # Print the given original list. print("The Given original list is :", gvn_lst) # Apply sort() function to the given list by passing the argument as # reverse = "True" to sort the given list elements in descending order. gvn_lst.sort(reverse=True) # Print the list after sorting all its elements in descending order. print("The given list after sorting in descending order: ") print(gvn_lst)
Output:
The Given original list is : ['hello', 'this', 'is', 'btechgeeks'] The given list after sorting in descending order: ['this', 'is', 'hello', 'btechgeeks']
3)Sort with custom function using key
If you want to create your own sorting implementation, the sort() method accepts a key function as an optional parameter.
You can sort the given list based on the results of the key function.
list.sort(key=len)
Below is the implementation:
# Taking element to sort def givenelement(element): return element[0] # Give the list as static input and store it in a variable. gvn_lst = [(1, 3), (4, 6), (2, 6), (5, 1)] # sort the given list with the key gvn_lst.sort(key=givenelement) # print the given list print('The given list after Sorting:', gvn_lst)
Output:
The given list after Sorting: [(1, 3), (2, 6), (4, 6), (5, 1)]
Method #2: Using Built-in Functions (User Input)
1) sort in Ascending Order
Approach:
- Give the list as user input using list(), input(),and split() functions.
- Store it in a variable.
- Print the given original list.
- Apply sort() function to the given list to sort the given list elements in ascending order.
- Print the list after sorting all its elements in ascending order.
- The Exit of Program.
Below is the implementation:
# Give the list as user input using list(),input(),and split() functions. # Store it in a variable. gvn_lst = list(input("Enter some random List Elements separated by spaces = ").split()) # Print the given original list. print("The Given original list is :", gvn_lst) # Apply sort() function to the given list to sort the given list elements in # ascending order. gvn_lst.sort() # Print the list after sorting all its elements in ascending order. print("The given list after sorting in ascending order: ", gvn_lst)
Output:
Enter some random List Elements separated by spaces = A R Q D I The Given original list is : ['A', 'R', 'Q', 'D', 'I'] The given list after sorting in ascending order: ['A', 'D', 'I', 'Q', 'R']
2) sort in Descending Order
Approach:
- Give the list as user input using list(), input(),and split() functions.
- Store it in a variable.
- Print the given original list.
- Apply sort() function to the given list by passing the argument as reverse = “True” to sort the given list elements in descending order.
- Print the list after sorting all its elements in ascending order.
- The Exit of Program.
Below is the implementation:
# Give the list as user input using list(),input(),and split() functions. # Store it in a variable. gvn_lst = list(input("Enter some random List Elements separated by spaces = ").split()) # Print the given original list. print("The Given original list is :", gvn_lst) # Apply sort() function to the given list by passing the argument as # reverse = "True" to sort the given list elements in descending order. gvn_lst.sort(reverse=True) # Print the list after sorting all its elements in descending order. print("The given list after sorting in descending order: ") print(gvn_lst)
Output:
Enter some random List Elements separated by spaces = good morning btechgeeks hello all The Given original list is : ['good', 'morning', 'btechgeeks', 'hello', 'all'] The given list after sorting in descending order: ['morning', 'hello', 'good', 'btechgeeks', 'all']
Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.