HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonTip

Sort a Python list by indexes

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
sortpythonindexeslist

Problem

If you want to sort a list based on another list containing the desired indexes, it's a fairly easy task, provided you know how to use the zip() and sorted() functions.
Using zip(), you'll combine the two lists into a list of tuples, where the first element of each tuple is the index from the second list, and the second element is the value from the first list.
Then, you'll use sorted() to sort the list of tuples based on the values of the indexes. Finally, you'll use a list comprehension to get the first element of each pair from the result. You can also use the reverse parameter to sort the dictionary in reverse order.
Finally, you can use a list comprehension to get the first element of each pair from the result.

Solution

def sort_by_indexes(lst, indexes, reverse=False):
  return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
          x[0], reverse=reverse)]

a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
b = [3, 2, 6, 4, 1, 5]
sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
sort_by_indexes(a, b, True)
# ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']


Then, you'll use sorted() to sort the list of tuples based on the values of the indexes. Finally, you'll use a list comprehension to get the first element of each pair from the result. You can also use the reverse parameter to sort the dictionary in reverse order.
Finally, you can use a list comprehension to get the first element of each pair from the result.

Code Snippets

def sort_by_indexes(lst, indexes, reverse=False):
  return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
          x[0], reverse=reverse)]

a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
b = [3, 2, 6, 4, 1, 5]
sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
sort_by_indexes(a, b, True)
# ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']

Context

From 30-seconds-of-code: sort-by-indexes

Revisions (0)

No revisions yet.