Sort Two Lists Together Python

Given two lists, sort the values of one list utilizing 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">

You watching: Sort two lists together python

Approach :Zip the 2 lists.Create a brand-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 objective of zip() is to map a similar index of multiple containers so that they can be supplied just utilizing as a solitary entity.Below is the implementation of the above approach:


Output:<"a", "d", "h", "b", "c", "e", "i", "f", "g"><"g", "k", "r", "e", "e", "g", "s", "f", "o">In the over code, we have actually 2 lists, the initially list is being sorted with respect to the values of the second list.y = < 0, 1, 1, 0, 1, 2, 2, 0, 1>Here initially the lowest worth is checked. Like in this list, 0 is the lowest, so beginning from the first index, 0 is the lowest and also it is at index 0. So the value of index 0 is stored at index 0 in the initially list. Similarly, 0 is again uncovered at index 3 and also so the value of index 3 in the first list is index 1. The same goes till the list is not completed.Approach 2: By making use of Thesaurus, list comprehension, lambda function
Output:<"a", "b", "c", "d", "e", "f", "g", "h", "i">Attention geek! Strengthen your structures through the Python Programming Foundation Course and also learn the basics. To begin with, your interview preparations Enhance your Data Structures ideas via the Python DS Course. And to begin via your Machine Learning Journey, sign up with the Machine Learning – Basic Level Course


https://media.sibbap.org/auth/avatar.png
DSA Course Class 9 to 12 School Students

See more: Book Review: ' Carry On Book Review S: Carry On: A Novel (Simon

DSA Self Paced Course
DSA Live Classes for Working Professionals
Read JSON file making use of PythonPython DictionaryReading and also Writing to message papers in PythonPython regime to convert a list to stringRead a document line by line in Python
Python regimen to transform a list to stringDefaultdict in PythonPython | Get dictionary secrets as a listHow to print without newline in Python?Python | Split string into list of characters
We use cookies to ensure you have actually the finest searching suffer on our website. By making use of our site, youacexpertise that you have actually check out and also construed ourCookie Policy & Privacy PolicyGot It !