Python - Extract records if Kth elements not in List
Last Updated :
30 Mar, 2023
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
# Python3 code to demonstrate working of
# Extract records if Kth elements not in List
# Using loop
# initializing list
test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)]
# printing original list
print("The original list : " + str(test_list))
# initializing arg. list
arg_list = [4, 8, 4]
# initializing K
K = 1
# Using set() to shorten arg list
temp = set(arg_list)
# loop to check for elements and append
res = []
for sub in test_list:
if sub[K] not in arg_list:
res.append(sub)
# printing result
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
# Python3 code to demonstrate working of
# Extract records if Kth elements not in List
# Using list comprehension + set()
# initializing list
test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)]
# printing original list
print("The original list : " + str(test_list))
# initializing arg. list
arg_list = [4, 8, 4]
# initializing K
K = 1
# Compiling set() and conditionals into single comprehension
res = [(key, val) for key, val in test_list if val not in set(arg_list)]
# printing result
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
# Python3 code to demonstrate working of
# Extract records if Kth elements not in List
# Using filter() + lambda function
# initializing list
test_list = [(5, 3), (7, 4), (1, 3), (7, 8), (0, 6)]
# printing original list
print("The original list : " + str(test_list))
# initializing arg. list
arg_list = [4, 8, 4]
# initializing K
K = 1
# Using set() to shorten arg list
temp = set(arg_list)
# using filter() + lambda function to extract elements
res = list(filter(lambda sub: sub[K] not in arg_list, test_list))
# printing result
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.
Similar Reads
Python - Extract Kth element of every Nth tuple in List Given list of tuples, extract Kth column element of every Nth tuple. Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 2, N = 3 Output : [3, 8, 10] Explanation : From 0th, 3rd, and 6th tuple, 2nd elements are 3, 8, 10. Input :test_list
8 min read
Extract Elements from a Python List When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print(
2 min read
Python | Every Kth element in list Sometimes, while working with Python lists, we can have a problem in which we require to extract every Kth element of list and slice out a new list from that. This type of problems are quite common as a variation of list slicing. Let's discuss a way in which this can be done. Method : Using list sli
3 min read
Python - Extract element from list succeeded by K Given a list, extract the elements which are having K as the next element. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5], K = 3 Output : [2, 5] Explanation : Elements before 3 are 2, 5. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 8], K = 8 Output : [7, 3] Explanation : Elements before 8 are 7, 3. Metho
5 min read
Python - Group records by Kth column in List Sometimes, while working with Python lists, we can have a problem in which we need to perform grouping of records on basis of certain parameters. One such parameters can be on the Kth element of Tuple. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + defaultdic
8 min read
Remove last K elements of list - Python Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python.Using list slicingList slicing is one of
3 min read