Sorting a list by another list in Python
Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python.
We will learn how to sort a given list based on the values in another list in this article.
Use the zip()
and sorted()
functions to sort a list based on another list in Python
In this method, we will use zip()
the function to create a third object by combining two given lists, the first one must be sorted and the second one depends on the sorting.
We can then use sorted()
the function, which extracts the first element of each pair of given lists from the sorted and zipped list.
A = ["r", "s", "t", "u", "v", "w", "x", "y", "z"]
B = [0, 1, 1, 0, 1, 2, 2, 0, 1]
result_list = [i for _, i in sorted(zip(B, A))]
print(result_list)
Output:
['r', 'u', 'y', 's', 't', 'v', 'z', 'w', 'x']
NumPy
Sort a list based on another list in Python
using the list_sort module
In this method, we convert the list into NumPy
an array and then apply the sorting algorithm to the list. We use argsort()
the function to sort the array on which the sorting depends and then use these values to filter the second array.
Refer to the following example.
import numpy
A = ["r", "s", "t", "u", "v", "w", "x", "y", "z"]
B = [0, 1, 1, 0, 1, 2, 2, 0, 1]
A = numpy.array(A)
B = numpy.array(B)
inds = B.argsort()
sorted_a = A[B]
print(sorted_a)
Output:
['r' 's' 's' 'r' 's' 't' 't' 'r' 's']
To get the final data in a list, use tolist()
the function.
more_itertools.sort_together
Sorting a list according to another list in Python
using
more_itertools
The module is itertools
an extension of the module. sort_together
The function returns the input iterables sorted together, taking key_list
the list in the argument as the sorting priority.
For example,
from more_itertools import sort_together
X = ["r", "s", "t", "u", "v", "w", "x", "y", "z"]
Y = [0, 1, 1, 0, 1, 2, 2, 0, 1]
s = sort_together([Y, X])[1]
print(list(s))
Output:
['r', 'u', 'y', 's', 't', 'v', 'z', 'w', 'x']
We need to use list()
the function to get the final result in list form.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding a string in a list in Python
Publish Date:2025/05/09 Views:75 Category:Python
-
This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem
Getting list shape in Python
Publish Date:2025/05/09 Views:139 Category:Python
-
In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim
Adding multiple elements to a list in Python
Publish Date:2025/05/09 Views:180 Category:Python
-
List is a mutable data structure in Python. It can contain values of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi
Get the index of the maximum and minimum values in a list in Python
Publish Date:2025/05/09 Views:117 Category:Python
-
In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi
List of numbers from 1 to N in Python
Publish Date:2025/05/09 Views:116 Category:Python
-
This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until
Convert List to Pandas DataFrame in Python
Publish Date:2025/05/09 Views:133 Category:Python
-
This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i
How to Find the Maximum Value in a List in Python
Publish Date:2025/05/09 Views:99 Category:Python
-
This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding
Convert a list to a set in Python
Publish Date:2025/05/08 Views:145 Category:Python
-
This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values in sequence. Python list stores comma separated values between
Remove the first element from a list in Python
Publish Date:2025/05/08 Views:173 Category:Python
-
This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which