Open In App

Python – Reverse Row sort in Lists of List

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we need to perform the sorting of rows of the matrix in descending order. This kind of problem has its application in the web development and Data Science domain. Let’s discuss certain ways in which this task can be performed.

Method #1: Using loop + sort() + reverse This problem can be solved using a loop to loop over each row. The sort and reverse can be used to perform the reverse sort of rows. 

Python3




# Python3 code to demonstrate
# Reverse Row sort in Lists of List
# using loop
 
# initializing list
test_list = [[4, 1, 6], [7, 8], [4, 10, 8]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Reverse Row sort in Lists of List
# using loop
for ele in test_list:
    ele.sort(reverse=True)
 
# printing result
print("The reverse sorted Matrix is : " + str(test_list))


Output

The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]

Time Complexity: O(n log n)
Auxiliary Space: O(1)

Method #2: Using list comprehension + sorted() This is yet another way in which this task can be performed. In this, we perform in a similar way, just pack the logic in one line using list comprehension to provide a compact alternative. 

Python3




# Python3 code to demonstrate
# Reverse Row sort in Lists of List
# using list comprehension + sorted()
 
# initializing list
test_list = [[4, 1, 6], [7, 8], [4, 10, 8]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Reverse Row sort in Lists of List
# using list comprehension + sorted()
res = [sorted(sub, reverse=True) for sub in test_list]
 
# printing result
print("The reverse sorted Matrix is : " + str(res))


Output

The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]

Time Complexity: O(n log n)
Auxiliary Space: O(n)

Method#3: Using map() + sorted This is one way to solve this problem. In this, we update the existing list with the help of map function which sorts the internal list in reverse order using the sorted function.

Python3




# Python3 code to demonstrate
# Reverse Row sort in Lists of List
# using map + sorted()
 
# initializing list
test_list = [[4, 1, 6], [7, 8], [4, 10, 8]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Reverse Row sort in Lists of List
# using map + sorted()
res = list(map(lambda x: sorted(x, reverse=True), test_list))
 
# printing result
print("The reverse sorted Matrix is : " + str(res))


Output

The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]

Time Complexity: O(n log n)
Auxiliary Space: O(n)

Method #4 : Using sort()+slicing

Python3




# Python3 code to demonstrate
# Reverse Row sort in Lists of List
# using loop
 
# initializing list
test_list = [[4, 1, 6], [7, 8], [4, 10, 8]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Reverse Row sort in Lists of List
# using loop
res=[]
for ele in test_list:
    ele.sort()
    res.append(ele[::-1])
 
# printing result
print("The reverse sorted Matrix is : " + str(res))


Output

The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]

Time Complexity: O(n log n)
Auxiliary Space: O(n)

Approach #5: Using map() function and lambda expression

The approach applies the sorted function with the reverse=True parameter to each row of the input matrix using map function and returns a list of lists as a result.

Step-by-step approach:

1. Apply the map() function to the input list of lists.
2. Apply a lambda expression to each row of the matrix to sort the row in descending order.
3. Convert the map object into a list and return it.

Python3




def reverse_sort_matrix(matrix):
    return list(map(lambda row: sorted(row, reverse=True), matrix))
matrix=[[4, 1, 6], [7, 8], [4, 10, 8]]
print(reverse_sort_matrix(matrix))


Output

[[6, 4, 1], [8, 7], [10, 8, 4]]

Time complexity: O(n^2 log n), where n is the number of elements in the matrix. The sorted() function has a time complexity of O(n log n), and it is being applied n times in this algorithm.
Auxiliary Space: O(n), where n is the number of elements in the matrix. We are creating a new matrix of the same size as the input matrix.

Method #6: Using the heapq module

Step-by-step approach:

  1. Import the heapq module using import heapq.
  2. Initialize the input list of lists test_list with some values.
  3. Print the original list test_list.
  4. Iterate over each sublist in the input list using a for loop.
  5. Apply the heapify() function of the heapq module on each sublist to convert it into a min-heap.
  6. Use a list comprehension to extract the minimum element from the heap using heappop() function in reverse order.
  7. Append the extracted elements to the result list res.
  8. Print the result list.

Python3




# Python3 code to demonstrate
# Reverse Row sort in Lists of List
# using the heapq module
 
import heapq
 
# initializing list
test_list = [[4, 1, 6], [7, 8], [4, 10, 8]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Reverse Row sort in Lists of List
# using the heapq module
res = []
for sublist in test_list:
    heapq.heapify(sublist)
    res.append([heapq.heappop(sublist) for i in range(len(sublist))][::-1])
 
# printing result
print("The reverse sorted Matrix is : " + str(res))


Output

The original list is : [[4, 1, 6], [7, 8], [4, 10, 8]]
The reverse sorted Matrix is : [[6, 4, 1], [8, 7], [10, 8, 4]]

Time complexity: O(N * M * logM) where N is the number of sublists and M is the maximum length of the sublists.
Auxiliary space: O(M) where M is the maximum length of the sublists.



Last Updated : 03 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads