 Open in App
Not now

# Python – Filter rows with required elements

• Last Updated : 08 Mar, 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))`

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.

My Personal Notes arrow_drop_up