Python – Filter Tuple with Elements capped on K
Last Updated :
12 Apr, 2023
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 : [(4, 5, 3), (4, 3, 2)] Explanation : All tuples have maximum value 7.
Method #1 : Using loop
In this, we iterate through all tuple elements, if element found greater than K, then tuple is flagged and not added in result list.
Python3
test_list = [( 4 , 5 , 3 ), ( 3 , 4 , 7 ), ( 4 , 3 , 2 ), ( 4 , 7 , 8 )]
print ( "The original list is : " + str (test_list))
K = 5
res_list = []
for sub in test_list:
res = True
for ele in sub:
if ele > K:
res = False
break
if res:
res_list.append(sub)
print ( "The filtered tuples : " + str (res_list))
|
Output
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
The filtered tuples : [(4, 5, 3), (4, 3, 2)]
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 #2 : Using all() + list comprehension
In this, we check for all the elements to be at max K using all(), if yes, then those tuples are added to result.
Python3
test_list = [( 4 , 5 , 3 ), ( 3 , 4 , 7 ), ( 4 , 3 , 2 ), ( 4 , 7 , 8 )]
print ( "The original list is : " + str (test_list))
K = 5
res = [sub for sub in test_list if all (ele < = K for ele in sub)]
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
The filtered tuples : [(4, 5, 3), (4, 3, 2)]
Method 3 : using filter() method with lambda function.
Steps to solve the problem :
- Define a lambda function to check if all elements of the tuple are less than or equal to K.
- Use filter() method to filter the tuples from the list based on the lambda function.
- Convert the filtered result to a list.
Python3
test_list = [( 4 , 5 , 3 ), ( 3 , 4 , 7 ), ( 4 , 3 , 2 ), ( 4 , 7 , 8 )]
print ( "The original list is : " + str (test_list))
K = 5
res = list ( filter ( lambda x: all (ele < = K for ele in x), test_list))
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
The filtered tuples : [(4, 5, 3), (4, 3, 2)]
Time complexity: O(n*k), where n is the number of tuples in the list and k is the number of elements in each tuple.
Auxiliary space: O(n), where n is the number of tuples in the list.
Method 4: Using reduce() method from functools module
- Import the reduce() method from the functools module using the statement from functools import reduce.
- Create a list of tuples named test_list. This list contains four tuples, each with three integer elements.
- Print the original list using the print() function and the str() function to convert the list to a string.
- Initialize a variable K to the integer value 5. This variable will be used to filter the tuples.
- Define a function named filter_tuples that takes a tuple as its argument. The function checks whether all the elements of the tuple are less than or equal to the value of K. If all the elements are less than or equal to K, the function returns True. Otherwise, it returns False.
- Use the reduce() method to filter the tuples. The reduce() method applies a function to the elements of an iterable in a cumulative way and returns a single value. In this case, we are using reduce() to combine the tuples that meet the given condition into a single list. The reduce() method takes three arguments:
Python3
from functools import reduce
test_list = [( 4 , 5 , 3 ), ( 3 , 4 , 7 ), ( 4 , 3 , 2 ), ( 4 , 7 , 8 )]
print ( "The original list is : " + str (test_list))
K = 5
def filter_tuples(tpl):
return all (ele < = K for ele in tpl)
res = reduce ( lambda acc, x: acc + [x] if filter_tuples(x) else acc, test_list, [])
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
The filtered tuples : [(4, 5, 3), (4, 3, 2)]
The time complexity : O(n*m), where n is the length of the input list, and m is the length of the largest tuple in the list.
The auxiliary space complexity : O(n), where n is the length of the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...