
- 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 – Filter Tuples with Integers
When it is required to filter tuple with integers, a simple iteration and the ‘not’ operator and the ‘isinstance’ method is used.
Example
Below is a demonstration of the same −
my_tuple = [(14, 25, "Python"), (5, 6), (3, ), ("cool", )] print("The tuple is :") print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub: if not isinstance(element, int): temp = False break if temp : my_result.append(sub) print("The result is :") print(my_result)
Output
The tuple is : [(14, 25, 'Python'), (5, 6), (3,), ('cool',)] The result is : [(5, 6), (3,)]
Explanation
A list of tuple is defined and is displayed on the console.
An empty list is created.
The list is iterated over, and the ‘isinstance’ method is used to see if the element belong to integer type.
If yes, a Boolean value is assigned to ‘False’.
The control breaks out of the loop.
Depending on the value of Boolean value, the element is appended to the empty list.
This is the output that is displayed on the console.
- Related Articles
- Python – Filter Tuples with Strings of specific characters
- Python – Filter unique valued tuples
- Python – Filter consecutive elements Tuples
- Python – Filter Tuples Product greater than K
- Filter Tuples by Kth element from List in Python
- Filter tuples according to list element presence in Python
- Python - Filter out integers from float numpy array
- Python – Filter all uppercase characters from given list of tuples
- Initialize tuples with parameters in Python
- Python program to find Tuples with positive elements in List of tuples
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python - Filter Pandas DataFrame with numpy
- Python – Filter dictionaries with ordered values
- Python – Extract tuples with elements in Range

Advertisements