Given two lists, sort the values of one list using the second list.
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']
- 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.
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:
['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, 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
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Largest, Smallest, Second Largest, Second Smallest in a List
- Python | Replace elements in second list with index of same element in first list
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Python | Sort a list according to the second element in sublist
- Python program to sort a list of tuples by second Item
- Python - Sort by Frequency of second element in Tuple List
- Python - Find first element by second in tuple List
- Python - Group first elements by second elements in Tuple list
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python - Assign values to Values List
- Calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis
- Python | Visualize missing values (NaN) values using Missingno Library
- Python - Rearrange elements second index greater than first
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Sort list containing alphanumeric values
- Python | Ways to sort a zipped list by values
- Python | Ways to sort list of float values
- Python - Sort List by Dictionary values
- Python - Sort Dictionary key and values List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.