Sort List by Indexes

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Sorting a list based on another list containing the desired indexes is a common task in programming. In this challenge, you will be tasked with creating a function that takes two lists as arguments and returns a new list sorted based on the indexes of the second list.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/FileHandlingGroup -.-> python/file_reading_writing("`Reading and Writing Files`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13718{{"`Sort List by Indexes`"}} python/booleans -.-> lab-13718{{"`Sort List by Indexes`"}} python/for_loops -.-> lab-13718{{"`Sort List by Indexes`"}} python/lists -.-> lab-13718{{"`Sort List by Indexes`"}} python/tuples -.-> lab-13718{{"`Sort List by Indexes`"}} python/function_definition -.-> lab-13718{{"`Sort List by Indexes`"}} python/default_arguments -.-> lab-13718{{"`Sort List by Indexes`"}} python/lambda_functions -.-> lab-13718{{"`Sort List by Indexes`"}} python/file_reading_writing -.-> lab-13718{{"`Sort List by Indexes`"}} python/build_in_functions -.-> lab-13718{{"`Sort List by Indexes`"}} end

Sort List by Indexes

Write a function sort_by_indexes(lst, indexes, reverse=False) that takes two lists as arguments and returns a new list sorted based on the indexes of the second list. The function should have the following parameters:

  • lst: A list of elements to be sorted.
  • indexes: A list of integers representing the desired indexes to sort the lst by.
  • reverse: An optional boolean parameter that, when set to True, sorts the list in reverse order.

The function should return a new list sorted based on the indexes of the second list.

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']

Summary

In this challenge, you were tasked with creating a function that sorts a list based on another list containing the desired indexes. By using the zip() and sorted() functions, you were able to combine and sort the two lists based on the values of indexes. You then used a list comprehension to get the first element of each pair from the result and returned the sorted list.

Other Python Tutorials you may like