Get the indices of sorted List in Python

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.

Advertisements

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:

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top