Python | Indices of sorted list of list elements

Sorting is a common construct and there have been many variations of it being discussed. But sometimes, we need to perform the sorting on the list of list and moreover just require to find the order in which element occurs before getting sorted. Let’s find out how to get indices of sorted order in list of lists.

Method #1 : Using List comprehension + enumerate() + sort()

The combination of above 3 functions can be used to perform this particular task. In this, we perform the sorting taking triplets consisting of element and row and column coordinates and return them in 2nd step.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Indices of sorted list of list elements
# using List comprehension + enumerate() + sort()
  
# initializing list
test_list = [[4, 5, 1],
             [9, 3, 2],
             [8, 6]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using List comprehension + enumerate() + sort()
# Indices of sorted list of list elements
res = [(i, j) for i, x in enumerate(test_list)
                     for j, k in enumerate(x)]
  
res.sort(key = lambda ij: test_list[ij[0]][ij[1]]) 
  
# print result
print("The indices of sorted order are : " + str(res))

chevron_right


Output :

The original list : [[4, 5, 1], [9, 3, 2], [8, 6]]
The indices of sorted order are : [(0, 2), (1, 2), (1, 1), (0, 0), (0, 1), (2, 1), (2, 0), (1, 0)]



 

Method #2 : Using sorted() + lambda

The task performed above can be performed as arguments to the sorted function and lambda function performs the task of list comprehension function as above.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Indices of sorted list of list elements
# using sorted() + lambda
  
# initializing list
test_list = [[4, 5, 1],
             [9, 3, 2],
             [8, 6]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using sorted() + lambda
# Indices of sorted list of list elements
res = sorted([(i, j) for i, x in enumerate(test_list)
                            for j, k in enumerate(x)],
             key = lambda ij: test_list[ij[0]][ij[1]])
  
# print result
print("The indices of sorted order are : " + str(res))

chevron_right


Output :

The original list : [[4, 5, 1], [9, 3, 2], [8, 6]]
The indices of sorted order are : [(0, 2), (1, 2), (1, 1), (0, 0), (0, 1), (2, 1), (2, 0), (1, 0)]



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.