
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to Order Tuples using external List
When it is required to order the tuples using an external list, the list comprehension, and ‘dict’ method can be used.
Below is the demonstration of the same −
Example
my_list = [('Mark', 34), ('Will', 91), ('Rob', 23)] print("The list of tuple is : ") print(my_list) ordered_list = ['Will', 'Mark', 'Rob'] print("The ordered list is :") print(ordered_list) temp = dict(my_list) my_result = [(key, temp[key]) for key in ordered_list] print("The ordered tuple list is : ") print(my_result)
Output
The list of tuple is : [('Mark', 34), ('Will', 91), ('Rob', 23)] The ordered list is : ['Will', 'Mark', 'Rob'] The ordered tuple list is : [('Will', 91), ('Mark', 34), ('Rob', 23)]
Explanation
The list of tuple is defined, and is displayed on the console.
Another list is defined, and is displayed on the console.
The list of tuple is converted into a dictionary.
It is iterated over and the elements are stored in another list.
This results in an ordered tuple which is displayed as output on the console.
- Related Articles
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to find Tuples with positive elements in List of tuples
- Python program to find Tuples with positive elements in a List of tuples
- Python program to sort a list of tuples alphabetically
- Python program to sort tuples in increasing order by any key.
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Update a list of tuples using another list in Python
- Python program to convert a list of tuples into Dictionary
- Python program to sort a list of tuples by second Item
- Python program to convert elements in a list of Tuples to Float
- Combining tuples in list of tuples in Python
- Sort Tuples in Increasing Order by any key in Python program
- Convert list of tuples to list of list in Python
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Flatten Tuples List to String in Python

Advertisements