Python | Sort the values of first list using second list

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’]
Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :

  1. Zip the two lists.
  2. Create a new, sorted list based on the zip using sorted().
  3. Using a list comprehension extract the first elements of each pair from the sorted, zipped list.

Concept :

The purpose of zip() is to map the similar index of multiple containers so that they can be used just using as single entity.

Below is the implementation of above approach:

# Python program to sort 
# one list using 
# the other list 

def sort_list(list1, list2): 

	zipped_pairs = zip(list2, list1) 

	z = [x for _, x in sorted(zipped_pairs)] 
	
	return z 
	

# driver code 
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']

In the above code, we have two lists, first list is being sorted with respect to the values of second list.

y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1]

Here first the lowest value are checked.Like in this list, 0 is lowest, so starting from first index, 0 is lowest and it is at index 0. So value of index 0 is stored at index 0 in the first list. Similarly, 0 is again found at index 3 and so value of index 3 in first list is index 1. The same goes until the list is not completed.

Refer: https://www.geeksforgeeks.org/python-sort-values-first-list-using-second-list/