Last updated: Apr 10, 2024
Reading timeยท3 min

To get the indices of a sorted list:
range() class to create a range object of the list's length.sorted() function to get the indices of the sorted list.key argument to specify the sorting criteria.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']

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.
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.
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.
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.
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.
The same approach can be used to get the indices of a sorted list of numbers.
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]
Alternatively, you can use the numpy.argsort() method.
The numpy.argsort() method returns the indices that would sort an array-like
object.
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.
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.
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.
You can also use the enumerate() function to get the indices of a sorted list.
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.
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).
You can learn more about the related topics by checking out the following tutorials: