# How to get the Indices of a sorted List in Python

Last updated: Apr 10, 2024
3 min

## #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
```Copied!```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']
``````

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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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']
``````

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

shell
```Copied!```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
```Copied!```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
```Copied!```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 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
```Copied!```my_list = ['bobby', 'hadz', 'com']

for index, item in enumerate(my_list):
print(index, item)  # ๐๏ธ 0 bobby, 1 hadz, 2 com
``````

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).

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