Open In App

Sort List of Lists Ascending and then Descending in Python

When you want to organize data effectively, arranging lists of lists becomes important. This guide will show you how to first sort a list of lists from smallest to largest and then arrange them in the opposite order, from largest to smallest. Learn an easy and efficient way to do this using Python, making data management smoother.

Example 1:



Input: List = [[2, 8, 10], [12, 45, 2], [4, 10, 1]]  
Output: Ascending [[2, 8, 10], [4, 10, 1], [12, 45, 2]]
Descending [[12, 45, 2], [4, 10, 1], [2, 8, 10]]

Sort List Of Lists Ascending And Then Descending

Below, are the methods to Sort a List Of Lists Ascending And Then Descending in Python.

Sort List Of Lists Ascending And Then Descending Using naive Method

Ascending: The for loop iterates the inner lists. If the condition checks if the first element in the current list is greater than the next list or not. If it is greater, then it will swap the lists.



Descending: The for loop iterates the inner lists. If the condition checks if the first element in the current list is less than the next list or not. If it is less, then it will swap the lists.




List = [[2, 8, 10], [12, 45, 2], [4, 10, 1]] 
for i in range(len(List)-1):
    # if the next element is greater then the next element, swap it.
    if List[i][0]>List[i+1][0]:
        List[i][0],List[i+1][0]=List[i+1][0],List[i][0]
print("Ascending",List)
 
 
List = [[2, 8, 10], [12, 45, 2], [4, 10, 1]] 
for i in range(len(List)-1):
    # if the next element is less then the next element, swap it.
    if List[i][0]<List[i+1][0]:
        List[i][0],List[i+1][0]=List[i+1][0],List[i][0]
print("Descending",List)

Output
Ascending [[2, 8, 10], [4, 45, 2], [12, 10, 1]]
Descending [[12, 8, 10], [4, 45, 2], [2, 10, 1]]

Sort List Of Lists Ascending And Then Descending Using sort() Method

In this example, sort() function will arrange the list of lists in ascending order by default. When we give reverse=True, then it will arrange the list of lists in descending order.




List = [[2, 8, 10], [12, 45, 2], [4, 10, 1]]
 
List.sort()
print("Ascending", List)
 
List.sort(reverse=True)
print("Descending", List)

Output
Ascending [[2, 8, 10], [4, 10, 1], [12, 45, 2]]
Descending [[12, 45, 2], [4, 10, 1], [2, 8, 10]]

Sort List Of Lists Ascending And Then Descending Using sorted()

In this example, sorted() function will arrange the list of lists in ascending order by default. When we give reverse=True, then it will arrange the list of lists in descending order.




List = [[2, 8, 10], [12, 45, 2], [4, 10, 1]]
#sorting list using sorted function
#Ascending
print("Ascending",sorted(List))
#Descending
print("Descending", sorted(List, reverse= True))

Output
Ascending [[2, 8, 10], [4, 10, 1], [12, 45, 2]]
Descending [[12, 45, 2], [4, 10, 1], [2, 8, 10]]

Sort List Of Lists Ascending And Then Descending Using Lambda Function

Here we used lambda expression which indicates sorting should be done based on which column. Ex: lambda x:x[1] indicates sorting should be done based on second column.




List = [[2, 8, 10], [12, 45, 2], [4, 10, 1]] 
 
List.sort(key=lambda x:x[1])
 
print("Sorting by 2nd column in Ascending", List)
List.sort(key=lambda x:x[1], reverse=True)
print("Sorting by 2nd column in Descending", List)

Output
Sorting by 2nd column in Ascending [[2, 8, 10], [4, 10, 1], [12, 45, 2]]
Sorting by 2nd column in Descending [[12, 45, 2], [4, 10, 1], [2, 8, 10]]


Article Tags :