Python – Filter Tuples with All Even Elements
Last Updated :
15 Apr, 2023
Given List of tuples, filter only those with all even elements.
Input : test_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 1, 2), (7, )]
Output : [(6, 4, 2, 8)]
Explanation : Only 1 tuple with all even elements.
Input : test_list = [(6, 4, 2, 9), (5, 6, 7, 6), (8, 1, 2), (7, )]
Output : []
Explanation : No tuple with all even elements.
Method #1: Using loop
In this, we iterate each tuple, and check for all even elements using % operator and if even one element is odd, tuple is flagged and not added in result list.
Python3
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
res_list = []
for sub in test_list:
res = True
for ele in sub:
if ele % 2 ! = 0 :
res = False
break
if res:
res_list.append(sub)
print ( "Filtered Tuples : " + str (res_list))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7, )]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time complexity: O(n*m), where n is the number of tuples in the list and m is the maximum number of elements in a tuple.
Auxiliary space: O(k), where k is the number of tuples that satisfy the condition.
Method #2 : Using all() + list comprehension
In this, the task of checking for all elements to be even is done using all(), and list comprehension is used for task of filtering post checking.
Python3
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all (ele % 2 = = 0 for ele in sub)]
print ( "Filtered Tuples : " + str (res))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7, )]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method#3: Using Recursive method.
Python3
def even_tuples(lst,newlst = [],start = 0 ):
if start = = len (lst):
return newlst
for i in lst[start]:
if i % 2 ! = 0 :
return even_tuples(lst,newlst,start + 1 )
else :
newlst.append(lst[start])
return even_tuples(lst,newlst,start + 1 )
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
res_list = even_tuples(test_list)
print ( "Filtered Tuples : " + str (res_list))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #4: Using lambda function and filter()
This implementation uses the lambda function to check if all elements in each tuple are even, and the filter() function to filter out tuples that don’t meet the criteria. Finally, the filtered tuples are converted to a list and printed.
Step by step approach :
- Initialize a list of tuples test_list with each tuple containing some integers.
- Print the original list test_list using the print statement.
- Define a lambda function using the lambda keyword. The lambda function takes an argument x and checks if all elements in x are even or not.
- Apply the filter() function on the test_list using the lambda function defined above as the first argument to the filter function.
- Convert the filtered output to a list using the list() function and store it in the variable res_list.
- Print the filtered list res_list using the print statement.
Python3
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
res_list = list ( filter ( lambda x: all (i % 2 = = 0 for i in x), test_list))
print ( "Filtered Tuples : " + str (res_list))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time complexity: O(n*k), where n is the number of tuples in the list and k is the length of the largest tuple
Auxiliary space: O(n*k), as the filtered tuples are stored in a new list that can potentially be as large as the original list.
Method #6: Using map() and all()
- Step 1: Initialize an empty list called “res_list” that will store the filtered tuples with all even elements.
- Step 2: Define a function called “all_even” that takes a tuple as its input and returns True if all elements of the tuple are even, and False otherwise.
- Step 3: Use the map() function to apply the all_even() function to each tuple in the “test_list”.
- Step 4: Use the all() function to check if all elements of each resulting map object are True.
- Step 5: If all elements of a tuple are True, append that tuple to the “res_list”.
- Step 6: Print the filtered list.
Python3
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
def all_even(t):
return all (i % 2 = = 0 for i in t)
res_list = [t for t in test_list if all ( map (all_even, [t]))]
print ( "Filtered Tuples : " + str (res_list))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time complexity: O(n^2), where n is the length of the original list. This is because we are using nested loops to iterate through the tuples and their elements.
Auxiliary space: O(m), where m is the number of tuples with all even elements. This is because we are storing only the filtered tuples in the “res_list”.
Method 6: Using the reduce() function from the functools module
- Initialize a list of tuples test_list containing some test data.
- Print the original list to the console.
- Import the functools module, which contains the reduce() function.
- Use the filter() function to apply a lambda function to each tuple in the test_list as the lambda function takes each tuple as an input argument and applies the reduce() function to check if all its elements are even. The reduce() function takes two input arguments, x, and y, and returns the logical AND of x and (y % 2 == 0). The initial value of x is True. The filter() function returns a new list containing only the tuples for which the lambda function returns True. Finally, convert the result into a list.
- Print the filtered tuples to the console.
Python3
import functools
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
res_list = list ( filter ( lambda t: functools. reduce (
lambda x, y: x and (y % 2 = = 0 ), t, True ), test_list))
print ( "Filtered Tuples : " + str (res_list))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time Complexity: O(n*m), where n is the number of tuples in the list and m is the maximum length of a tuple in the list.
Auxiliary Space: O(1).
Method 7: Using heapq:
Algorithm:
- Initialize the input list “test_list”.
- Use the “filter” function along with a lambda function to iterate through each tuple in “test_list”.
- For each tuple, use the “all” function to check if all elements in the tuple are even.
- If all elements are even, keep the tuple in the filtered list, otherwise discard it.
- Convert the filtered list into a regular Python list using the “list” function.
- Print the filtered list.
Python3
import heapq
test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )]
print ( "The original list is : " + str (test_list))
res_list = list ( filter ( lambda t: all (x % 2 = = 0 for x in t), test_list))
print ( "Filtered Tuples : " + str (res_list))
|
Output
The original list is : [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,)]
Filtered Tuples : [(6, 4, 2, 8), (8, 0, 2)]
Time Complexity:
The time complexity of the code depends on the size of the input list “test_list”. The lambda function inside the filter function iterates through each tuple in the list, and the “all” function checks if all elements of the tuple are even. Since the worst-case time complexity of the “all” function is O(n), where n is the length of the tuple, the total time complexity of the code is O(nm), where n is the average length of the tuples in “test_list” and m is the length of “test_list”. Therefore, the time complexity of the code is O(nm).
Space Complexity:
The space complexity of the code is also dependent on the size of the input list “test_list”. The filtered list will be at most the same size as the input list, and since we’re creating a new list to store the filtered tuples, the space complexity is O(m), where m is the length of “test_list”. Therefore, the space complexity of the code is O(m).
Similar Reads
Python - Filter tuple with all same elements
Given List of tuples, filter tuples that have same values. Input : test_list = [(5, 6, 5, 5), (6, 6, 6), (9, 10)] Output : [(6, 6, 6)] Explanation : 1 tuple with same elements. Input : test_list = [(5, 6, 5, 5), (6, 5, 6), (9, 10)] Output : [] Explanation : No tuple with same elements. Method #1 : U
4 min read
Python - Filter Tuple with Elements capped on K
Given a List of Tuples, extract tuples in which each element is max K. Input : test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)], K = 7 Output : [(4, 5, 3), (3, 4, 7), (4, 3, 2)] Explanation : All tuples have maximum value 7. Input : test_list = [(4, 5, 3), (4, 3, 2), (4, 7, 8)], K = 7 Output
5 min read
Python - Filter rows with required elements
Given a Matrix, filter rows with required elements from other list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]], check_list = [4, 6, 2, 8] Output : [[2, 4, 6], [2, 4, 8]] Explanation : All elements are from the check list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4,
5 min read
Python - Extract tuples with elements in Range
Given list of tuples, extract tuples having elements in range. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output :
4 min read
Python - Filter consecutive elements Tuples
Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
5 min read
Python - Filter Tuples with Integers
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 : Usi
5 min read
Python - Get Even indexed elements in Tuple
Sometimes, while working with Python data, one can have a problem in which we need to perform the task of extracting just even indexed elements in tuples. This kind of problem is quite common and can have possible application in many domains such as day-day programming. Let's discuss certain ways in
5 min read
Python - Elements with same index
Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9] Output : [1, 4, 6] Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9] Output : [] Explanation : No number at its index. Method #1:
5 min read
Python - Test for all Even elements in the List for the given Range
Given a List of elements, test if all elements are even in a range. Input : test_list = [3, 1, 4, 6, 8, 10, 1, 9], i, j = 2, 5 Output : True Explanation : 4, 6, 8, 10, all are even elements in range. Input : test_list = [3, 1, 4, 6, 87, 10, 1, 9], i, j = 2, 5 Output : False Explanation : All not eve
2 min read
Python - Tuple elements inversions
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple elements. This is an essential utility as we come across bitwise operations many times. Letâs discuss certain ways in which this task can be performed. Method #1 : Using map() + l
3 min read