Python - Filter rows with required elements
Last Updated :
30 Apr, 2023
Given a Matrix, filter rows with required elements from other list.
Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]], check_list = [4, 6, 2, 8]
Output : [[2, 4, 6], [2, 4, 8]]
Explanation : All elements are from the check list.
Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]], check_list = [6, 2, 8]
Output : []
Explanation : No list with all elements from check list.
Method #1 : Using list comprehension + all()
In this, we perform iteration and filtering using list comprehension from list and check for all elements present in each row using all().
Python3
# Python3 code to demonstrate working of
# Filter rows with required elements
# Using list comprehension + all()
# initializing list
test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
# printing original list
print("The original list is : " + str(test_list))
# initializing check_list
check_list = [4, 6, 2, 8]
# using in operator to check for presence
res = [sub for sub in test_list if all(ele in check_list for ele in sub)]
# printing result
print("Filtered rows : " + str(res))
Output:
The original list is : [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]] Filtered rows : [[2, 4, 6], [2, 4, 8]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + all()
In this, the task of filtering is done using filter() and lambda, all() is used for the task of extracting all elements from that are present in the checklist.
Python3
# Python3 code to demonstrate working of
# Filter rows with required elements
# Using filter() + lambda + all()
# initializing list
test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
# printing original list
print("The original list is : " + str(test_list))
# initializing check_list
check_list = [4, 6, 2, 8]
# using in operator to check for presence
# filter(), getting all rows checking from check_list
res = list(filter(lambda sub : all(ele in check_list for ele in sub), test_list))
# printing result
print("Filtered rows : " + str(res))
Output:
The original list is : [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]] Filtered rows : [[2, 4, 6], [2, 4, 8]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. filter() + lambda + all() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #3: Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of
# Filter rows with required elements
import itertools
# initializing list
test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
# printing original list
print("The original list is : " + str(test_list))
# initializing check_list
check_list = [4, 6, 2, 8]
res = list(itertools.filterfalse(lambda sub : not all(ele in check_list for ele in sub), test_list))
# printing result
print("Filtered rows : " + str(res))
OutputThe original list is : [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
Filtered rows : [[2, 4, 6], [2, 4, 8]]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #4: Using set() and issubset() function:
Algorithm:
1.Initialize the given list and check list.
2.Convert the check list into set for efficient subset operation.
3.Loop through each sub-list of the given list.
4.Check if the set formed by the sub-list is a subset of the check set.
5.If yes, append the sub-list to the result list.
6.Return the result list.
Python3
test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
check_list = [4, 6, 2, 8]
# printing original list
print("The original list is : " + str(test_list))
# using set() and issubset() function to filter rows
check_set = set(check_list)
res = [sub for sub in test_list if check_set.issuperset(sub)]
# printing result
print("Filtered rows : ", res)
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
Filtered rows : [[2, 4, 6], [2, 4, 8]]
Time Complexity: O(n*m), where n is the number of sub-lists and m is the average length of sub-lists. This is because we are iterating through each sub-list once and performing a subset operation which takes O(m) time in the worst case.
Auxiliary Space: O(m), where m is the length of the check list. This is because we are storing the check set in memory.
Method #5: Using nested for loops
In this approach, we will use nested for loops to iterate over the test_list and check if each sublist is a subset of the check_list. If a sublist is a subset, we will append it to a new list called res.
Steps:
Initialize an empty list called res.
Iterate over each sublist in test_list using a for loop.
Initialize a variable called flag to True.
Iterate over each element in the sublist using another for loop.
If the element is not present in check_list, set the flag to False and break out of the inner for loop.
If the flag is still True after iterating over all elements of the sublist, append the sublist to res.
Print the filtered rows.
Python3
test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]]
check_list = [4, 6, 2, 8]
# using nested for loops to filter rows
res = []
for sub in test_list:
flag = True
for elem in sub:
if elem not in check_list:
flag = False
break
if flag:
res.append(sub)
# printing result
print("Filtered rows : ", res)
OutputFiltered rows : [[2, 4, 6], [2, 4, 8]]
Time complexity: O(N*M) where N is the number of sublists in test_list and M is the maximum length of any sublist.
Auxiliary space: O(K) where K is the number of sublists that are a subset of check_list.
Similar Reads
Python - Filter Rows with Range Elements
Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python - Rows with all List elements
Given a Matrix, get all the rows with all the list elements. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [1, 2] Output : [[2, 1, 8], [6, 1, 2]] Explanation : Extracted lists have 1 and 2. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [2
8 min read
Python - Filter tuple with all same elements
Given List of tuples, filter tuples that have same values. Input : test_list = [(5, 6, 5, 5), (6, 6, 6), (9, 10)] Output : [(6, 6, 6)] Explanation : 1 tuple with same elements. Input : test_list = [(5, 6, 5, 5), (6, 5, 6), (9, 10)] Output : [] Explanation : No tuple with same elements. Method #1 : U
4 min read
Python - Filter rows with Elements as Multiple of K
Given a Matrix, extract rows with elements multiple of K. Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4 Output : [[4, 8, 12]] Explanation : All are multiples of 4. Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4 Output : [] Explanation : No
6 min read
Python - Filter Tuple with Elements capped on K
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
5 min read
Python - Retain all K elements Rows
Sometimes, while working with Python lists, we can have a problem in which we need to retain rows which have only K as elements. This kind of application can occur in data domains which take Matrix as input. Let's discuss certain ways in which this task can be performed. Input : test_list = [[7, 6],
8 min read
Python - Extract tuples with elements in Range
Given list of tuples, extract tuples having elements in range. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output :
4 min read
Python - Remove rows with Numbers
Given a Matrix, remove rows with integer instances. Input : test_list = [[4, 'Gfg', 'best'], ['gfg', 5, 'is', 'best'], [3, 5], ['GFG', 'Best']] Output : [['GFG', 'Best']] Explanation : All rows with numbers are removed. Input : test_list = [[4, 'Gfg', 'best'], ['gfg', 5, 'is', 'best'], [3, 5], ['GFG
7 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] E
6 min read
Python | Remove similar element rows in tuple Matrix
Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This ta
6 min read