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 rows with all multiples of 4.
Method #1 : Using list comprehension + all()
In this, we check for all elements to be multiple using all(), and iteration of rows occur using list comprehension.
Python3
test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 5
res = [sub for sub in test_list if all (ele % K = = 0 for ele in sub)]
print ( "Rows with K multiples : " + str (res))
|
Output
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
Rows with K multiples : [[5, 10, 15], [100, 15]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1) additional space is not needed
Method #2 : Using filter() + lambda + all()
In this, we perform task of filtering using filter() and lambda function and task of checking for all elements in rows using all().
Python3
test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 5
res = list ( filter ( lambda sub : all (ele % K = = 0 for ele in sub), test_list))
print ( "Rows with K multiples : " + str (res))
|
Output
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
Rows with K multiples : [[5, 10, 15], [100, 15]]
Method #3:Using itertools.filterfalse() method
Python3
import itertools
test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 5
res = list (itertools.filterfalse( lambda sub : not all (ele % K = = 0 for ele in sub), test_list))
print ( "Rows with K multiples : " + str (res))
|
Output
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
Rows with K multiples : [[5, 10, 15], [100, 15]]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #4:Using the reduce() function and a lambda function with the all() function:
Algorithm :
- Import the reduce() function from the functools module and define the test_list and K variables.
- Use a lambda function with the filter() function to iterate through each sublist of test_list and check if all its elements are divisible by K.
- Print the result.
Python3
from functools import reduce
test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 5
res = list ( filter ( lambda x: reduce ( lambda a, b: a and b, [i % K = = 0 for i in x]), test_list))
print ( "Rows with K multiples : " + str (res))
|
Output
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
Rows with K multiples : [[5, 10, 15], [100, 15]]
The time complexity : O(n*m), where n is the number of sublists in test_list and m is the maximum length of a sublist. This is because the filter() function and the lambda function inside it are applied to each sublist.
The space complexity : O(k), where k is the number of sublists that satisfy the condition of having all their elements divisible by K. This is because a new list is created to store the filtered sublists, and the maximum size of this list is k.
Method 5 : using a for loop
- Initialize a list called “test_list” with multiple sublists.
- Print the original list.
- Initialize a variable called “K” with a value.
- Create an empty list called “res” to store the sublists that meet the condition.
- Iterate through each sublist in “test_list”.
- For each sublist, iterate through each element.
- Check if the element is divisible by K using the modulo operator (%). If any element is not divisible by K, set the flag variable to False and break out of the loop.
- If the flag is still True after checking all the elements in the sublist, it means all the elements are divisible by K. Append the sublist to the “res” list.
- After iterating through all the sublists, print the sublists in “res” that satisfy the condition.
Python3
test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 5
res = []
for sub in test_list:
flag = True
for ele in sub:
if ele % K ! = 0 :
flag = False
break
if flag:
res.append(sub)
print ( "Rows with K multiples : " + str (res))
|
Output
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
Rows with K multiples : [[5, 10, 15], [100, 15]]
Time complexity: O(n * m), where n is the number of sublists and m is the maximum number of elements in a sublist.
Auxiliary space: O(p), where p is the number of sublists that satisfy the condition. In the worst case, all sublists satisfy the condition, so the auxiliary space is O(n).
Last Updated :
02 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...