- 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 â€“ 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.
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)
The tuple is : [(14, 25, 'Python'), (5, 6), (3,), ('cool',)] The result is : [(5, 6), (3,)]
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