Open In App

Python – Filter rows with required elements

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

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))


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*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.


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*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)


Output

Filtered 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads