Python program to sort values of one list using second list

Python program to sort one list by using values of different list :

In this example program, we will learn how to sort elements of one list by using elements of a different list. Let’s take a look into the program :

Python Program :

first_list = ["O","X","A","C","D","K"]
second_list = ['1','2','3','4','5','6']

zipped_pairs = zip(first_list,second_list)

sorted_pairs = sorted(zipped_pairs)

result = [item[1] for item in sorted_pairs]

print(result)

Explanation :

Here, we have two list first_list and second_list. We are sorting the values of second_list using first_list. The output of the program is :

['3', '4', '5', '6', '1', '2']

Since, the value 3 is on third position of second_list and ‘A’ is on third position of first_list, it is placed on first position. Similarly, the other elements are also placed by comparing with the elements of first_list.

source

Leave a Reply