Open In App

Python – Filter Tuples with Integers

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given Tuple list, filter tuples which are having just int data type.

Input : [(4, 5, “GFg”), (3, ), (“Gfg”, )] 
Output : [(3, )] 
Explanation : 1 tuple (3, ) with all integral values. 

Input : [(4, 5, “GFg”), (3, “Best” ), (“Gfg”, )] 
Output : [] 
Explanation : No tuple with all integers.

Method #1 : Using loop + isinstance()

In this, we iterate the each tuple and check for data type other than int, using isinstance(), if found tuple is flagged off and omitted.

Python3




# Python3 code to demonstrate working of
# Filter Tuples with Integers
# Using loop + isinstance()
 
# initializing list
test_list = [(4, 5, "GFg"), (5, 6), (3, ), ("Gfg", )]
 
# printing original list
print("The original list is : " + str(test_list))
 
res_list = []
for sub in test_list:
    res = True
    for ele in sub:
         
        # checking for non-int.
        if not isinstance(ele, int):
            res = False
            break
    if res :
        res_list.append(sub)
         
# printing results
print("Filtered tuples : " + str(res_list))


Output

The original list is : [(4, 5, 'GFg'), (5, 6), (3, ), ('Gfg', )]
Filtered tuples : [(5, 6), (3, )]

Time Complexity: O(n^2), where n is the number of tuples in the list.
Auxiliary Space: O(n), as the filtered list is stored in res_list and its size is proportional to the number of tuples in the list.

Method #2 : Using all() + list comprehension + isinstance()

In this, all() is used to check if all elements are integers using isinstance(), if that checks, tuples are added to result.

Python3




# Python3 code to demonstrate working of
# Filter Tuples with Integers
# Using all() + list comprehension + isinstance()
 
# initializing list
test_list = [(4, 5, "GFg"), (5, 6), (3, ), ("Gfg", )]
 
# printing original list
print("The original list is : " + str(test_list))
 
# list comprehension to encapsulate in 1 liner
res = [sub for sub in test_list if all(isinstance(ele, int) for ele in sub)]
         
# printing results
print("Filtered tuples : " + str(res))


Output

The original list is : [(4, 5, 'GFg'), (5, 6), (3, ), ('Gfg', )]
Filtered tuples : [(5, 6), (3, )]

Time Complexity: O(n*n), where n is the length of the input list. 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 

Method 3: Use the filter() function along with lambda function 

Step-by-step approach:

  • Define a lambda function that takes a tuple as input and returns True if all elements of the tuple are integers, else False.
  • Use the filter() function to filter the tuples from the given list of tuples that satisfy the condition specified in the lambda function.
  • Convert the filtered result into a list and store it in a variable.
  • Print the filtered tuples.

Python3




# Python3 code to demonstrate working of
# Filter Tuples with Integers
# Using filter() and lambda function
 
# initializing list
test_list = [(4, 5, "GFg"), (5, 6), (3, ), ("Gfg", )]
 
# printing original list
print("The original list is : " + str(test_list))
 
# lambda function to check if all elements of tuple are integers
is_all_int = lambda tup: all(isinstance(ele, int) for ele in tup)
 
# using filter() to filter tuples with integers
res = list(filter(is_all_int, test_list))
 
# printing results
print("Filtered tuples : " + str(res))


Output

The original list is : [(4, 5, 'GFg'), (5, 6), (3,), ('Gfg',)]
Filtered tuples : [(5, 6), (3,)]

Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(k), where k is the number of tuples that satisfy the condition specified in the lambda function.

Method 4: Using reduce() 

Step-by-step approach:

  • Import the reduce() function from the functools module.
  • Define a lambda function that takes two arguments, a tuple and a boolean value, and returns True if all elements of the tuple are integers and the boolean value is True, and False otherwise.
  • Use the reduce() function to apply the lambda function to each tuple in the test_list and accumulate the results.
  • Use the filter() function to filter out the tuples that have a True result from the reduce() function.
  • Return the filtered list.

Python3




from functools import reduce
 
# initializing list
test_list = [(4, 5, "GFg"), (5, 6), (3, ), ("Gfg", )]
 
# printing original list
print("The original list is : " + str(test_list))
 
# lambda function to check if all elements of tuple are integers
is_all_int = lambda tup, bool_val: bool_val and all(isinstance(ele, int) for ele in tup)
 
# using reduce() and filter() to filter tuples with integers
res = list(filter(lambda x: reduce(is_all_int, (x, True)), test_list))
 
# printing results
print("Filtered tuples : " + str(res))


Output

The original list is : [(4, 5, 'GFg'), (5, 6), (3,), ('Gfg',)]
Filtered tuples : [(5, 6), (3,)]

Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, for the filtered list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads