How to get the Indices of a sorted List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Get the indices of a sorted list in Python
  2. Get the indices of a sorted list using numpy.argsort()
  3. Get the indices of a sorted list using enumerate()

# Get the indices of a sorted list in Python

To get the indices of a sorted list:

  1. Use the range() class to create a range object of the list's length.
  2. Use the sorted() function to get the indices of the sorted list.
  3. Set the key argument to specify the sorting criteria.
main.py
a_list = ['a', 'b', 'd', 'c'] indices = sorted( range(len(a_list)), key=lambda index: a_list[index] ) print(indices) # ๐Ÿ‘‰๏ธ [0, 1, 3, 2] sorted_list = [a_list[index] for index in indices] print(sorted_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

get indices of sorted list

The code for this article is available on GitHub
If you use numpy scroll down to the next subheading.

The indices list stores the indices that would sort the list.

The sorted() function takes an iterable and returns a new sorted list from the items in the iterable.

main.py
a_list = ['a', 'b', 'd', 'c'] sorted_list = sorted(a_list) print(sorted_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

We used the range() class to get a range object of the list's length.

main.py
a_list = ['a', 'b', 'd', 'c'] print(list(range(len(a_list)))) # ๐Ÿ‘‰๏ธ [0, 1, 2, 3]

The range() class is commonly used for looping a specific number of times.

The sorted() function takes an optional key argument that can be used to sort by different criteria.

main.py
a_list = ['a', 'b', 'd', 'c'] indices = sorted( range(len(a_list)), key=lambda index: a_list[index] ) print(indices) # ๐Ÿ‘‰๏ธ [0, 1, 3, 2]

The key argument can be set to a function that determines the sorting criteria.

We sort the indices, but the criteria we use is each value in the list.

The lambda function gets called with each index from the range object and uses the corresponding list item as sorting criteria.

You can use a list comprehension if you need to sort the list as well.

main.py
a_list = ['a', 'b', 'd', 'c'] indices = sorted( range(len(a_list)), key=lambda index: a_list[index] ) print(indices) # ๐Ÿ‘‰๏ธ [0, 1, 3, 2] sorted_list = [a_list[index] for index in indices] print(sorted_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

We used a list comprehension to iterate over the list of indices and returned each list item.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The same approach can be used to get the indices of a sorted list of numbers.

main.py
a_list = [1, 2, 4, 3] indices = sorted( range(len(a_list)), key=lambda index: a_list[index] ) print(indices) # ๐Ÿ‘‰๏ธ [0, 1, 3, 2] sorted_list = [a_list[index] for index in indices] print(sorted_list) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4]

# Get the indices of a sorted list using numpy.argsort()

Alternatively, you can use the numpy.argsort() method.

The numpy.argsort() method returns the indices that would sort an array-like object.

main.py
import numpy as np a_list = ['a', 'b', 'd', 'c'] indices = np.argsort(a_list) print(indices) # ๐Ÿ‘‰๏ธ [0 1 3 2] sorted_list = [a_list[index] for index in indices] print(sorted_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

get indices of sorted list using numpy argsort

The code for this article is available on GitHub

Make sure you have NumPy installed to be able to run the code sample.

shell
pip install numpy # ๐Ÿ‘‡๏ธ or with pip3 pip3 install numpy

The numpy.argsort() method takes an array-like object and returns the indices that would sort the array.

The indices variable stores the indices in an array, but you can use the tolist() method if you need to convert the array to a list.

main.py
import numpy as np a_list = ['a', 'b', 'd', 'c'] indices = np.argsort(a_list).tolist() print(indices) # ๐Ÿ‘‰๏ธ [0, 1, 3, 2] sorted_list = [a_list[index] for index in indices] print(sorted_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']

The tolist method converts an array to a list.

# Get the indices of a sorted list using enumerate()

You can also use the enumerate() function to get the indices of a sorted list.

main.py
a_list = ['a', 'b', 'd', 'c'] indices = [ tup[0] for tup in sorted(enumerate(a_list), key=lambda x: x[1]) ] print(indices) # ๐Ÿ‘‰๏ธ [0, 1, 3, 2] sorted_list = [a_list[index] for index in indices] print(sorted_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c', 'd']
The code for this article is available on GitHub

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐Ÿ‘‰๏ธ 0 bobby, 1 hadz, 2 com

get the indices of sorted list using enumerate

We sorted the enumerate object by the second element (the value) and used a list comprehension to iterate over the result.

On each iteration, we return the first element of the current tuple (the index).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev