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
# Python3 code to demonstrate working of # Filter Tuples with All Even Elements # Using loop # initializing list test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) res_list = [] for sub in test_list: res = True # check if all are even for ele in sub: if ele % 2 ! = 0 : res = False break if res: res_list.append(sub) # printing results print ( "Filtered Tuples : " + str (res_list)) |
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
# Python3 code to demonstrate working of # Filter Tuples with All Even Elements # Using all() + list comprehension # initializing list test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) # testing for tuple to be even using all() res = [sub for sub in test_list if all (ele % 2 = = 0 for ele in sub)] # printing results print ( "Filtered Tuples : " + str (res)) |
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
# Python3 code to demonstrate working of # Filter Tuples with All Even Elements # Using recursive method def even_tuples(lst,newlst = [],start = 0 ): if start = = len (lst): #base condition 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 ) # initializing list test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) res_list = even_tuples(test_list) # printing results print ( "Filtered Tuples : " + str (res_list)) #this code contributed by tvsk |
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
# Python3 code to demonstrate working of # Filter Tuples with All Even Elements # Using lambda function and filter() # initializing list test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) # using lambda function and filter to filter tuples with all even elements res_list = list ( filter ( lambda x: all (i % 2 = = 0 for i in x), test_list)) # printing results print ( "Filtered Tuples : " + str (res_list)) |
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
# Python3 code to demonstrate working of # Filter Tuples with All Even Elements # Using map() and all() # initializing list test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) # define function to check if all elements of a tuple are even def all_even(t): return all (i % 2 = = 0 for i in t) # use map() and all() to filter tuples with all even elements res_list = [t for t in test_list if all ( map (all_even, [t]))] # print results print ( "Filtered Tuples : " + str (res_list)) |
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
# Python program for the above approach # initializing list import functools test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) # import functools module # use reduce() and a lambda function to # filter tuples with all even elements res_list = list ( filter ( lambda t: functools. reduce ( lambda x, y: x and (y % 2 = = 0 ), t, True ), test_list)) # print results print ( "Filtered Tuples : " + str (res_list)) |
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 # initializing list test_list = [( 6 , 4 , 2 , 8 ), ( 5 , 6 , 7 , 6 ), ( 8 , 0 , 2 ), ( 7 , )] # printing original list print ( "The original list is : " + str (test_list)) # use heapq to filter tuples with all even elements res_list = list ( filter ( lambda t: all (x % 2 = = 0 for x in t), test_list)) # print results print ( "Filtered Tuples : " + str (res_list)) #This code is contrinuted by Jyothi pinjala. |
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).