Open In App
Related Articles

Python – Reverse sort Matrix Row by Kth Column

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 combination of above methods can be used to perform this task. In this, we sort the list in descending order using reverse by particular column using lambda function and list comprehension. 

Python3

# Python3 code to demonstrate
# Reverse sort Matrix Row by Kth Column
# using sorted() + lambda + reverse()
 
# Initializing list
test_list = [['Manjeet', 65], ['Akshat', 42], ['Akash', 38], ['Nikhil', 192]]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Initializing K
K = 1
 
# Reverse sort Matrix Row by Kth Column
# using sorted() + lambda + reverse()
res = sorted(test_list, key = lambda ele: ele[K], reverse = True)
 
# printing result
print ("List after performing sorting of matrix records : " + str(res))

                    
Output : 

The original list is : [[‘Manjeet’, 65], [‘Akshat’, 42], [‘Akash’, 38], [‘Nikhil’, 192]] List after performing sorting of matrix records : [[‘Nikhil’, 192], [‘Manjeet’, 65], [‘Akshat’, 42], [‘Akash’, 38]]

Time complexity: O(mlogn+nlogn), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements

Method #2 : Using sort() + itemgetter() The combination of above methods can also be used to solve this problem. In this, we perform the task of lambda function using itemgetter and sort() performs sorting. 

Python3

# Python3 code to demonstrate
# Reverse sort Matrix Row by Kth Column
# using sort() + itemgetter()
from operator import itemgetter
 
# Initializing list
test_list = [['Manjeet', 65], ['Akshat', 42], ['Akash', 38], ['Nikhil', 192]]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Initializing K
K = 1
 
# Reverse sort Matrix Row by Kth Column
# using sort() + itemgetter()
test_list.sort(key = itemgetter(K), reverse = True)
 
# printing result
print ("List after performing sorting of matrix records : " + str(test_list))

                    
Output : 

The original list is : [[‘Manjeet’, 65], [‘Akshat’, 42], [‘Akash’, 38], [‘Nikhil’, 192]] List after performing sorting of matrix records : [[‘Nikhil’, 192], [‘Manjeet’, 65], [‘Akshat’, 42], [‘Akash’, 38]]

Method 3: Using a list comprehension

Step-by-step approach:

  • We first create a list comprehension that sorts the rows of the matrix based on their Kth column in descending order.
  • Use the sorted() function with a list comprehension that sorts the rows of the matrix based on their Kth column in descending order. The key function for the sort is a lambda function that returns the Kth column of each row.
  • The sorted() function returns a list of tuples, where the first element of each tuple is the Kth column of the corresponding row and the second element is the entire row.
  • Finally, we use another list comprehension to extract only the rows from the sorted list of tuples and create a new list of rows sorted by the Kth column in descending order.

Python3

# Initializing list
test_list = [['Manjeet', 65], ['Akshat', 42], ['Akash', 38], ['Nikhil', 192]]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# Initializing K
K = 1
 
# Reverse sort Matrix Row by Kth Column
# using list comprehension
test_list = [row for _, row in sorted([(x[K], x) for x in test_list], reverse=True)]
 
# printing result
print("List after performing sorting of matrix records : " + str(test_list))

                    

Output
The original list is : [['Manjeet', 65], ['Akshat', 42], ['Akash', 38], ['Nikhil', 192]]
List after performing sorting of matrix records : [['Nikhil', 192], ['Manjeet', 65], ['Akshat', 42], ['Akash', 38]]

Time complexity: O(n log n), where n is the number of rows in the matrix. 
Auxiliary space: O(n), where n is the number of rows in the matrix.



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