In this article, we will discuss different ways to get the indices of sorted List in Python.
Table Of Contents
Introduction
Suppose we have a list of numbers,
listOfNumbers = [22, 78, 54, 32, 93, 80, 74]
We want a list of indices that will sort this List. Like,
[0, 3, 2, 6, 1, 5, 4]
If we access the elements of first list, by the order of indices given in the above list, then we will get elements from first list in sorted order. There are different ways to do this. Let’s discuss them one by one.
Frequently Asked:
Get the indices of sorted List using sorted() function
Get a sequence of numbers from 0 to N. Where N is the size of List. Sort these numbers using sorted() function, but with custom comparator. Along with the list, pass a Lambda function to the sorted() function as custom comparator. This lambda function accepts an index position as argument, and returns the value at the given index position in list. Sorted() function will use this lambda function as comparator, while comparing index positions in the given list. It will return a list of indices that would sort the given list. Let’s see an example,
# List of numbers listOfNumbers = [22, 78, 54, 32, 93, 80, 74] # Get a list of indices that would sort the given list indices = sorted( range(len(listOfNumbers)), key = lambda index : listOfNumbers[index]) print(indices)
Output:
[0, 3, 2, 6, 1, 5, 4]
It returned a list of indices that would sort the given list. If we print the list element by the order of indices in the above list. Then it will print list elements in sorted order. For example,
# Access list element by the indices List for index in indices: print(listOfNumbers[index])
Output:
Latest Python - Video Tutorial
22 32 54 74 78 80 93
Get the indices of sorted List using enumerate() & sorted()
Pass a list to the enumerate() function, and get a list of pairs. Each pair contains an index position, and value at that index positions. Sort this list of pairs based on second value, using the sorted() function. It will give us the indices of sorted List. For example,
# List of numbers listOfNumbers = [22, 78, 54, 32, 93, 80, 74] # Get a list of indices that would sort the given list indices = [ elem[0] for elem in sorted( enumerate(listOfNumbers), key = lambda pair : pair[1] )] print(indices)
Output:
[0, 3, 2, 6, 1, 5, 4]
It returned a list of indices that would sort the given list. If we print the list element by the order of indices in the above list. Then it will print list elements in sorted order. For example,
# Access list element by the indices List for index in indices: print(listOfNumbers[index])
Output:
22 32 54 74 78 80 93
Get the indices of sorted List using NumPy
NumPy module provides a function argsort(). Which accepts a squence, and returns an array of indices that would sort the given sequence. Then convert this array of indices to a list. For example,
import numpy as np # List of numbers listOfNumbers = [22, 78, 54, 32, 93, 80, 74] # Get a list of indices that would sort the given list indices = np.argsort(listOfNumbers).tolist() print(indices)
Output:
[0, 3, 2, 6, 1, 5, 4]
It returned a list of indices that would sort the given list. If we print the list element by the order of indices in the above list. Then it will print list elements in sorted order. For example,
# Access list element by the indices List for index in indices: print(listOfNumbers[index])
Output:
22 32 54 74 78 80 93
Summary
We learned how to get the indices of sorted List in Python. Thanks.
Latest Video Tutorials