# Python | Relative sorted order in Matrix

• Last Updated : 11 May, 2020

Sometimes, while working with Python Matrix, we can have data arranged randomly and we can have a requirement in which we need to get the element position in sorted order of Matrix. Let’s discuss a certain way in which this task can be performed.

Method : Using list comprehension + `enumerate() + sort() + lambda`
The solution to problem can be achieved using the combination of above functions. In these, we need to first arrange element for index value tuple creation using` enumerate()` and list comprehension. Then, we employ sort function to perform custom sort using lambda function.

Code :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

 `# Python3 code to demonstrate working of``# Relative sorted order in Matrix``# using list comprehension + enumerate() + sort() + lambda`` ` `# initialize list``test_list ``=` `[[``1``, ``3``, ``1``], [``4``, ``6``], [``7``, ``8``, ``10``]]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# Relative sorted order in Matrix``# using list comprehension + enumerate() + sort() + lambda``res ``=` `[(i, j) ``for` `i, x ``in` `enumerate``(test_list) ``for``       ``j, _ ``in` `enumerate``(x)]``res.sort(key ``=` `lambda` `f: test_list[f[``0``]][f[``1``]])`` ` `# printing result``print``(``"Sorted order of Matrix elements : "` `+` `str``(res))`
Output :
```The original list is : [[1, 3, 1], [4, 6], [7, 8, 10]]
Sorted order of Matrix elements : [(0, 0), (0, 2), (0, 1), (1, 0),
(1, 1), (2, 0), (2, 1), (2, 2)]
```
My Personal Notes arrow_drop_up