# Python – Reverse Row sort in Lists of List

Sometimes, while working with data, we can have a problem in which we need to perform the sorting of rows of matrix in descending order. This kind of problem has its application in 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 reverse sort of rows.

 `# 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]]
```

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

 `# 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]]
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.