- 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 Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python â€“ Ordered tuples extraction
When it is required to extract ordered tuples, a list comprehension, the ‘sorted’ method, the ‘tuple’ method and the ‘==’ operator is used.
Below is a demonstration of the same −
my_list = [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42,65, 56), (99, 91)] print("The list is :") print(my_list) my_result = [element for element in my_list if tuple(sorted(element)) == element] print("The result is :") print(my_result)
The list is : [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42, 65, 56), (99, 91)] The result is : [(13, 24, 56)]
A list of integers is defined and is displayed on the console.
A list comprehension is used to iterate over the list, and every element is sorted and converted to a tuple and compared to the element.
If they are equal, it is converted to a list, and is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Rear element extraction from list of tuples records in Python
- How to split Python tuples into sub-tuples?
- Combining tuples in list of tuples in Python
- Count tuples occurrence in list of tuples in Python
- How to Concatenate tuples to nested tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Updating Tuples in Python
- Compare tuples in Python
- Python â€“ Extend consecutive tuples
- What are Ordered dictionaries in Python?
- Python â€“ Filter dictionaries with ordered values
- How to create Ordered dictionaries in Python?
- Basic Tuples Operations in Python
- Python - Column summation of tuples
- Remove matching tuples in Python