Tricky Question on Python list

Could someone explain this question to me.It’s Confusing,

QUESTION

  1. Given two lists, sort the values of the first list using the second list.

Examples:

Input : list_1 = [“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”]
list_2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]

Output :[‘a’, ‘d’, ‘h’, ‘b’, ‘c’, ‘e’, ‘i’, ‘f’, ‘g’]

Input : list_1 = [“g”, “e”, “e”, “k”, “s”, “f”, “o”, “r”, “g”, “e”, “e”, “k”, “s”]
list_2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]

Output : [‘g’, ‘k’, ‘r’, ‘e’, ‘e’, ‘g’, ‘s’, ‘f’, ‘o’]

In the first example, we have 9 letters and 9 numbers. The numbers can be considered as being paired off with the letters. This means the 1st, 4th and 8th letters will be paired off with 0 (based on their position in the list). The 2nd, 3rd, 5th and 9th letters will be paired off with 1. The 6th and 7th letters will be paired off with 2. If the pairs are sorted based on ascending numbers, then the corresponding letters will give the output posted by you.

In the second example, we have 13 letters and 9 numbers. Here, the first 9 letters will be paired off with the 9 numbers, while the last 4 remaining letters (“e”, “e”, “k”, “s”) will be ignored. Similar to the first case, sorting is to be done based on the numbers. If the letters have equal weight, then they will be ordered alphabetically. e.g. In the second example, the 2nd, 3rd, 5th and 9th letters will be paired off with 1.
These letters will be “e”, “e”, “s”, “g”. They all have equal weight (i.e. 1), so an alphabetical ordering will result in the order “e”, “e”, “g”, “s”. You can see that in the middle of the second example’s output.

You didn’t post a link, so I don’t know if there are other criteria to consider. But based on the two examples, the question can be solved in one line.

1 Like

I have tried to solve it a couple of times, but haven’t got the right solution.
Could you please send the code , so that I could analyse it and improve my problem solving skills.

I am a Beginner.

If you want to share your attempts, perhaps you will get some feedback or suggestions about your approaches.
I don’t know if you have progressed to the lesson about list comprehensions yet. My solution uses list comprehension, but the same can be accomplished with a loop. In that case, the code will be longer than a line though. I don’t know the complete question wording, but based on the two examples above, here is one possible way to solve the problem:

Spoiler
ordered_list = [entry[1] for entry in sorted(zip(list_2, list_1))]

Basically, here is what is happening in this line of code:

  • First we use the zip function to pair off the two lists. The default behavior of the zip function is to stop when the shorter list is exhausted. See zip documentation for more details.

  • Then we use the sorted function to sort the zipped iterable. Notice I chose zip(list_2, list_1) instead of zip(list_1, list_2) deliberately, because I wanted the pairs to be (0, "a"), (1, "b"), ... instead of ("a", 0), ("b", 1), .... The benefit of my choice is that I don’t have to provide the sorted function with any key. If I chose zip(list_1, list_2), then I won’t be able to rely on the default sorting and would have to provide a suitable key to the sorted function.

  • The sorted function will sort the tuple entries based on the first element of each tuple. In cases where the first element is same, then the second element of the tuple will be used as a tie-breaker. So, when the number is the same, then the alphabetical order of the second element will break the tie.

  • entry[1] will pick out the second element of each tuple in the sorted iterable. The list comprehension will provide us a list which I have assigned to the variable ordered_list

Some useful Codecademy articles:
https://www.codecademy.com/article/list-comprehension
https://www.codecademy.com/article/zip-for-lists

2 Likes

Using the zip function, as @mtrtmk suggested, is definitely something to look into.

list_1 = ["a", "b", "c", "d", "e", "f", "g", "h"] list_2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1] print(list_1) zipped = list(zip(list_2, list_1)) #zip zipped.sort() #sorts zipped (which is a list of tuples) pair_of_lists = list(zip(*zipped)) #unzip result = list(pair_of_lists[1]) # second thin in the pair print(result)

Yes, you could compress the those 4 lines into one line if you really wanted to.

Note that I didn’t actually change the original list, I made a new list
(but sorting a list usually means changing the original list).

2 Likes