
- 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 find Tuples with positive elements in List of tuples
When it is required to find tuples that have position elements from a list of tuples, list comprehension can be used.
Below is a demonstration of the same −
Example
my_list = [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] print("The list is : ") print(my_list) my_result = [sub for sub in my_list if all(elem >= 0 for elem in sub)] print("The positive elements are : ") print(my_result)
Output
The list is : [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] The positive elements are : [(56, 43), (24, 56)]
Explanation
A list of tuples is defined, and is displayed on the console.
A list comprehension can be used to iterate through the elements.
It is checked to see if every element is greater than 0 or not.
If it is greater than 0, it is converted to a list element, and is stored in a variable.
This variable is displayed as output on the console.
- Related Articles
- Python program to find Tuples with positive elements in a List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python program to convert elements in a list of Tuples to Float
- Combining tuples in list of tuples in Python
- Python program to Convert a elements in a list of Tuples to Float
- Count tuples occurrence in list of tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Find top K frequent elements from a list of tuples in Python
- Find Dissimilar Elements in Tuples in Python
- Python – Extract tuples with elements in Range
- Python program to sort a list of tuples alphabetically
- Find the tuples containing the given element from a list of tuples in Python
- Python program to Order Tuples using external List
- Python - Change the signs of elements of tuples in a list
- Convert list of tuples to list of list in Python

Advertisements