Open In App

Python – Reverse Row sort in Lists of List

Last Updated : 03 May, 2023
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.



Similar Reads

Python | Reverse Sort Row Matrix integration
Often during problem solving we come across to many problems where we need to reverse sort the list. But sometimes we would also want to reverse sort another list so that the elements of are automatically shifted and remain at same index as the first list even after first list get reverse sorted. Let’s discuss certain ways in which this can be done
7 min read
Python - Reverse sort Matrix Row by Kth Column
Sometimes, while working with data, we can have a problem in which we need to perform sorting of each row of records by some of decisive factor like score. This kind of problem is common in competitive programming and web development. Lets discuss certain ways in which this task can be performed. Method #1 : Using sorted() + lambda + reverse The co
4 min read
Python - Concatenate two list of lists Row-wise
Given two matrices, the task is to write a Python program to add elements to each row from initial matrix. Input : test_list1 = [[4, 3, 5,], [1, 2, 3], [3, 7, 4]], test_list2 = [[1, 3], [9, 3, 5, 7], [8]] Output : [[4, 3, 5, 1, 3], [1, 2, 3, 9, 3, 5, 7], [3, 7, 4, 8]] Explanation : Matrix is row wise merged. Input : test_list1 = [[4, 3, 5,], [1, 2,
5 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Python Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order
Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2 Input: a: NULL b: 2->3->20 Output: res: 20->3->2Recommended: Please solve it on "PRACTIC
4 min read
Python | Sort list of lists by lexicographic value and then length
There are many times different types of sorting has been discussed in python lists. The sorting of python list of lists has also been discussed. But sometimes, we have two parameters upon which we need to sort. First one being the list sum and next being its length. Let's discuss how this type of problem can be solved. Method #1 : Using sort() twic
6 min read
Python | Sort list of lists by the size of sublists
Given a list of lists, the task is to sort a list on the basis of size of sublists. Let's discuss a few methods to do the same. Method #1: Using sort C/C++ Code # Python code to demonstrate # sort list of list # on the basis of size of sublist ini_list = [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 4, 6]] # printing initial ini_list print
5 min read
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:
3 min read
Python Program to Reverse Every Kth row in a Matrix
Given a Matrix, the task is to write a python program to reverse every Kth row. Where K is some span value. Input : test_list = [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]], K = 4 Output : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [6, 3], [3, 7, 4], [2, 9]] Explanation : Every 4th row is reversed.Input : test_list = [[5, 3, 2], [8, 6, 3], [
6 min read
Python | Reverse Order Sort in String List
Sometimes, while working with Python, we can have a problem in which we need to perform the reverse sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + sorted() + join()
3 min read
three90RightbarBannerImg