
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How can I sort one list by values from another list in Python?
We can sort a list by values from another list by setting up the 2nd list as index numbers for the values in the 1st List in sorted order.
Sort a List by values from another list
Example
In this example, we will sort a list by values from another list i.e. the 2nd list will have the index in the order in which they are placed in sorted order −
# Two Lists list1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] list2 = [2, 5, 1, 4, 3] print("List1 = \n",list1) print("List2 (indexes) = \n",list2) # Sorting the List1 based on List2 res = [val for (_, val) in sorted(zip(list2, list1), key=lambda x: x[0])] print("\nSorted List = ",res)
Output
List1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] List2 (indexes) = [2, 5, 1, 4, 3] Sorted List = ['Audi', 'BMW', 'Hyundai', 'Tesla', 'Toyota']
Sort a list by values from another list using Dictionary
Example
In this example, we will sort a list by values another list. We will add both the lists in a Dictionary and then usethe key value in Dictionary with lambda to sort.
# Two Lists list1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] list2 = [2, 5, 1, 4, 3] print("List1 = \n",list1) print("List2 (indexes) = \n",list2) # Blank dictionary newDict = {} # Blank list outList = [] # Both the lists in our dictionary newDict newDict = {list1[i]: list2[i] for i in range(len(list2))} # Sorting using the sorting() based on key and values # Using lambda as well s = {k: v for k, v in sorted(newDict.items(), key=lambda item: item[1])} # Element addition in the list for i in s.keys(): outList.append(i) print("\nSorted List = \n",outList)
Output
List1 = ['BMW', 'Toyota', 'Audi', 'Tesla', 'Hyundai'] List2 (indexes) = [2, 5, 1, 4, 3] Sorted List = ['Audi', 'BMW', 'Hyundai', 'Tesla', 'Toyota']
- Related Articles
- How do I sort a list of dictionaries by values of the dictionary in Python?
- Python program to mask a list using values from another list
- Ways to sort list of dictionaries by values in Python
- How to remove index list from another list in python?
- How do I insert all elements from one list into another in Java?
- Sort list of dictionaries by values in C#
- Python | Sort the values of first list using second list
- Ways to sort list of dictionaries by values in Python Using itemgetter
- Sort Dictionary key and values List in Python
- Python - First occurrence of one list in another
- Java Program to copy value from one list to another list
- How can I remove the same element in the list by Python
- Ways to sort list of dictionaries by values in Python Using lambda function
- Python - Insert list in another list
- Python – Sort by Units Digit in a List

Advertisements