Quick Reach
Python list length method
Python comes up with a very easy method to sort list. This method is sort()..
Syntax of sort method
list_name.sort()
Simple example of using sort Alphabetically
In example below sort method is used for an existing list. It will sort out list by alphabetical order.
1
2
3
4
5
6
7
|
list_sort = [‘c’,‘b’,‘a’]
print (“list before sorting= “, list_sort)
list_sort.sort()
print (“list after sorting= “, list_sort)
|
Output will be
list before sorting= [‘c’, ‘b’, ‘a’]
list after sorting= [‘a’, ‘b’, ‘c’]
Sort Example with descending order
1
2
3
4
5
6
7
|
list_sort = [‘a’,‘b’,‘c’]
print (“list before sorting= “, list_sort)
list_sort.sort(reverse=True)
print (“list after sorting= “, list_sort)
|
Output will be:
list before sorting= [‘a’, ‘b’, ‘c’]
list after sorting= [‘c’, ‘b’, ‘a’]
Example with numbers in descending order
1
2
3
4
5
6
7
|
list_sort = [10,5,6,8]
print (“list before sorting= “, list_sort)
list_sort.sort(reverse=True)
print (“list after sorting= “, list_sort)
|
Output will be:
list before sorting= [10, 5, 6, 8]
list after sorting= [10, 8, 6, 5]
Leave A Comment?