Sort the values of first list using second list in Python
Last Updated :
13 Apr, 2023
Given two lists, sort the values of one list using the second list.
Examples:
Input : list1 = [“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”]
list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
Output : [‘a’, ‘d’, ‘h’, ‘b’, ‘c’, ‘e’, ‘i’, ‘f’, ‘g’]
Input : list1 = [“g”, “e”, “e”, “k”, “s”, “f”, “o”, “r”, “g”, “e”, “e”, “k”, “s”]
list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
Output : [‘g’, ‘k’, ‘r’, ‘e’, ‘e’, ‘g’, ‘s’, ‘f’, ‘o’]
Approach :
- Zip the two lists.
- Create a new, sorted list based on the zip using sorted().
- Using a list comprehension extract the first elements of each pair from the sorted, zipped list.
Concept :
The purpose of zip() is to map a similar index of multiple containers so that they can be used just using as a single entity.
Below is the implementation of the above approach:
Python
def sort_list(list1, list2):
zipped_pairs = zip (list2, list1)
z = [x for _, x in sorted (zipped_pairs)]
return z
x = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ]
y = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
print (sort_list(x, y))
x = [ "g" , "e" , "e" , "k" , "s" , "f" , "o" , "r" , "g" , "e" , "e" , "k" , "s" ]
y = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
print (sort_list(x, y))
|
Output
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
['g', 'k', 'r', 'e', 'e', 'g', 's', 'f', 'o']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
In the above code, we have two lists, the first list is being sorted with respect to the values of the second list.
y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
Here first the lowest value is checked. Like in this list, 0 is the lowest, so starting from the first index, 0 is the lowest and it is at index 0. So the value of index 0 is stored at index 0 in the first list. Similarly, 0 is again found at index 3 and so the value of index 3 in the first list is index 1. The same goes until the list is not completed.
Approach 2: By using Dictionary, list comprehension, lambda function
Python3
def sorting_of_element(list1, list2):
f_1 = {}
final_list = []
f_1 = {list1[i]: list2[i] for i in range ( len (list2))}
f_lst = {k: v for k, v in sorted (f_1.items(), key = lambda item: item[ 1 ])}
for i in f_lst.keys():
final_list.append(i)
return final_list
list1 = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ]
list2 = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
list3 = sorting_of_element(list1, list2)
print (list3)
|
Output
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Approach 3: Using sort(),list() and set() methods
Python3
list1 = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ]
list2 = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
a = list ( set (list2))
a.sort()
res = []
for i in a:
for j in range ( 0 , len (list2)):
if (list2[j] = = i):
res.append(list1[j])
print (res)
|
Output
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
New Approach : Using collections.OrderedDict()
Using OrderedDict() to store the mapping of list1 and list2, sorted() is used to sort the OrderedDict() on the basis of values from the second list and finally list() is used to get the result.
Python3
from collections import OrderedDict
list1 = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ]
list2 = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
d = OrderedDict( zip (list1, list2))
res = list (OrderedDict( sorted (d.items(), key = lambda x: x[ 1 ])))
print (res)
|
Output
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Approach : Using the numpy.argsort() function with fancy indexing
note: install numpy module using command “pip install numpy”
The given methods are quite efficient, but we can use the numpy.argsort() function with fancy indexing to solve the problem more efficiently. Here is how we can implement it:
Algorithm:
Import numpy library.
Define a function sort_list that takes two lists list1 and list2 as input.
Get the indices that would sort the list2 using np.argsort(list2) and store it in idx.
Convert list1 to a numpy array, then use idx to sort it and return it.
Python3
import numpy as np
def sort_list(list1, list2):
idx = np.argsort(list2)
return np.array(list1)[idx]
x = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" ]
y = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
print (sort_list(x, y))
x = [ "g" , "e" , "e" , "k" , "s" , "f" , "o" , "r" , "g" , "e" , "e" , "k" , "s" ]
y = [ 0 , 1 , 1 , 0 , 1 , 2 , 2 , 0 , 1 ]
print (sort_list(x, y))
|
Output:
[‘a’ ‘d’ ‘h’ ‘b’ ‘c’ ‘e’ ‘i’ ‘f’ ‘g’]
[‘g’ ‘k’ ‘r’ ‘e’ ‘e’ ‘s’ ‘g’ ‘f’ ‘o’]
Time Complexity: O(nlogn) due to sorting operation using np.argsort().
Auxiliary Space: O(n) as we create a numpy array with the same size as list1.
This implementation is efficient as it leverages the optimized numpy library for sorting the array using argsort() function.
Please Login to comment...