Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - SORT LIST ELEMENTS


How to sort a List in Ascending/Descending Order?


The 'sort( )' Function is used to sort a List in Ascending/Descending order.


At first let us see an example to sort a List in Ascending order.


Example :


x = ["Mohan", "Kriti", "Salim"] 
x.sort()
print("The Sorted List in ascending order is ",x) 


Output :



  The Sorted List in ascending order is ['Kriti', 'Mohan', 'Salim']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Then we have used the 'sort( )' method to sort the List 'x' in ascending order.


x.sort( )

And the List 'x' gets sorted with 'Kriti' as the first value, 'Mohan' second and 'Salim' as the third.


java_Collections

And we get the below output.


The Sorted List in ascending order is ['Kriti', 'Mohan', 'Salim']

Next let us see, how to sort a List in Descending order.


Example :


x = ["Mohan", "Kriti", "Salim"] 
x.sort(reverse = True)
print("The Sorted List in descending order is ",x) 


Output :



  The Sorted List in descending order is ['Salim', 'Mohan', 'Kriti']

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the List,


java_Collections

Then we have used the parameter 'reverse = True' along with the 'sort( )' method to sort the List 'x' in descending order.


x.sort(reverse = True)

And the List 'x' gets sorted in descending order with 'Salim' as the first value, 'Mohan' second and 'Kriti' as the third.


java_Collections

And we get the below output.


The Sorted List in descending order is ['Salim', 'Mohan', 'Kriti']

Also it is a better option to convert the elements in the List in lower case. So the case sensitive problems don't occur.


We can just use the parameter 'key = str.lower' with the 'sort( )' Function.


Let the see the example to sort in Ascending order.


Example :


x = ["Mohan", "Kriti", "Salim"] 
x.sort(key = str.lower)
print("The Sorted List in ascending order is ",x) 


Output :



  The Sorted List in ascending order is ['Kriti', 'Mohan', 'Salim']

How to sort a List with numbers in Increasing/Deceasing Order?


Even here the 'sort( )' Function is used to sort a List with numbers in Increasing/Deceasing Order.


At first let us see an example to sort a List with numbers in Increasing order.


Example :


x = [5, 3, 2, 4] 
x.sort()
print("The Sorted List in increasing order is ",x) 


Output :



  The Sorted List in increasing order is [2, 3, 4, 5]

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = [5, 3, 2, 4]

Below is how the values are positioned in the List,


java_Collections

Then we have used the 'sort( )' method to sort the List 'x' in increasing order.


x.sort()

And the numbers in the List 'x' gets sorted.


java_Collections

And we get the below output.


The Sorted List in increasing order is [2, 3, 4, 5]

Next let us see, how to sort a List in Descending order.


Example :


x = [5, 3, 2, 4]
x.sort(reverse = True)
print("The Sorted List in decreasing order is ",x) 


Output :



  The Sorted List in decreasing order is [5, 4, 3, 2]

So, in the above code we have created a 'List' and initialised to the variable 'x'.


x = [5, 3, 2, 4]

Below is how the values are positioned in the List,


java_Collections

Then we have used the parameter 'reverse = True' along with the 'sort( )' method to sort the List 'x' in decreasing order.


x.sort(reverse = True)

And the List 'x' gets sorted in decreasing order.


java_Collections

And we get the below output.


The Sorted List in decreasing order is [5, 4, 3, 2]