Open In App

Python – Filter Tuple with Elements capped on K

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Filter Tuple with Elements capped on K
# Using loop
 
# initializing list
test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
res_list = []
for sub in test_list:
    res = True
    for ele in sub:
 
        # check if any element is greater than K
        if ele > K:
            res = False
            break
    if res:
        res_list.append(sub)
 
# printing result
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




# Python3 code to demonstrate working of
# Filter Tuple with Elements capped on K
# Using all() + list comprehension
 
# initializing list
test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# using all() to check for each tuple being in K limit
res = [sub for sub in test_list if all(ele <= K for ele in sub)]
 
# printing result
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 :

  1. Define a lambda function to check if all elements of the tuple are less than or equal to K.
  2. Use filter() method to filter the tuples from the list based on the lambda function.
  3. Convert the filtered result to a list.

Python3




# Python3 code to demonstrate working of
# Filter Tuple with Elements capped on K
# Using filter() method with lambda function
 
# initializing list
test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# using filter() method with lambda function to filter the tuples
res = list(filter(lambda x: all(ele <= K for ele in x), test_list))
 
# printing result
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

  1. Import the reduce() method from the functools module using the statement from functools import reduce.
  2. Create a list of tuples named test_list. This list contains four tuples, each with three integer elements.
  3. Print the original list using the print() function and the str() function to convert the list to a string.
  4. Initialize a variable K to the integer value 5. This variable will be used to filter the tuples.
  5. 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.
  6. 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




# Python3 code to demonstrate working of
# Filter Tuple with Elements capped on K
# Using reduce() method from functools module
 
from functools import reduce
 
# initializing list
test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# defining a function to filter tuples based on the given condition
def filter_tuples(tpl):
    return all(ele <= K for ele in tpl)
 
# using reduce() method to filter the tuples
res = reduce(lambda acc, x: acc + [x] if filter_tuples(x) else acc, test_list, [])
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads