Given list of tuples, task is to extract all the tuples where Kth index elements are not present in argument list.
Input : test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)], arg_list = [6, 8, 8], K = 1
Output : [(5, 3), (7, 4), (1, 3)]
Explanation : All the elements which have either 6 or 8 at 1st index are removed.
Input : test_list = [(5, 3), (7, 4)], arg_list = [3, 3, 3, 3], K = 1
Output : [(7, 4)]
Explanation : (5, 3) is removed as it has 3 at 1st index.
Method #1 : Using set() + loop
This is one way in which this task can be. In this, we shorten the argument list using set and then efficiently check for Kth index having any element from arg. list and append accordingly.
Python3
test_list = [( 5 , 3 ), ( 7 , 4 ), ( 1 , 3 ), ( 7 , 8 ), ( 0 , 6 )]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 8 , 4 ]
K = 1
temp = set (arg_list)
res = []
for sub in test_list:
if sub[K] not in arg_list:
res.append(sub)
print ( "Extracted elements : " + str (res))
|
OutputThe original list : [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)]
Extracted elements : [(5, 3), (1, 3), (0, 6)]
Time complexity: O(n), where n is the length of the test_list. The set() + loop takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using list comprehension + set()
This is yet another way in which this task can be performed. In this, we compile both, task of filtering duplicates using set() and compiling elements using conditionals inside list comprehension.
Python3
test_list = [( 5 , 3 ), ( 7 , 4 ), ( 1 , 3 ), ( 7 , 8 ), ( 0 , 6 )]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 8 , 4 ]
K = 1
res = [(key, val) for key, val in test_list if val not in set (arg_list)]
print ( "Extracted elements : " + str (res))
|
OutputThe original list : [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)]
Extracted elements : [(5, 3), (1, 3), (0, 6)]
Time complexity: O(n), where n is the number of tuples in test_list.
Auxiliary space: O(k), where k is the number of tuples in res list.
Method #3: Using filter() + lambda function
- The filter() function takes a function and an iterable as arguments, and returns an iterator that contains the elements from the iterable for which the function returns True.
- Use a lambda function as the first argument to filter(), which takes a tuple sub and checks if its Kth element is not in the arg_list.
- The second argument to filter() is the test_list.
- The result of filter() is an iterator, so convert it to a list using the list() function and assign it to res.
- Finally, print the extracted elements.
Python3
test_list = [( 5 , 3 ), ( 7 , 4 ), ( 1 , 3 ), ( 7 , 8 ), ( 0 , 6 )]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 8 , 4 ]
K = 1
temp = set (arg_list)
res = list ( filter ( lambda sub: sub[K] not in arg_list, test_list))
print ( "Extracted elements : " + str (res))
|
OutputThe original list : [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)]
Extracted elements : [(5, 3), (1, 3), (0, 6)]
Time complexity: O(n), where n is the number of tuples in the test_list.
Auxiliary space: O(1), as we are not creating any additional data structures apart from the res list to store the extracted elements.