- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- 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
- Python - Filter out integers from float numpy array
- Filter Tuples by Kth element from List in Python
- Filter tuples according to list element presence in Python
- Python – Filter all uppercase characters from given list of tuples
- Initialize tuples with parameters in Python
- Python - Filter Pandas DataFrame with numpy
- 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 – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Filter dictionaries with ordered values