Open In App

Python – Filter rows with Elements as Multiple of K

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Access element at Kth index in String
# Using list comprehension + all()
 
# initializing string list
test_list = [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
res = [sub for sub in test_list if all(ele % K == 0 for ele in sub)]
 
# printing result
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




# Python3 code to demonstrate working of
# Access element at Kth index in String
# Using filter() + lambda + all()
 
# initializing string list
test_list = [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# using all() to check for all elements being multiples of K
res = list(filter(lambda sub : all(ele % K == 0 for ele in sub), test_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Access element at Kth index in String
import itertools
 
# initializing string list
test_list = [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# using all() to check for all elements being multiples of K
res = list(itertools.filterfalse(lambda sub : not all(ele % K == 0 for ele in sub), test_list))
 
# printing result
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 :

  1. Import the reduce() function from the functools module and define the test_list and K variables.
  2. 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.
  3. Print the result.

Python3




from functools import reduce
# initializing string list
test_list = [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
# printing original list
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))
# printing result
print("Rows with K multiples : " + str(res))
#This code is contributed by Jyothi pinjala.


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

  1. Initialize a list called “test_list” with multiple sublists.
  2. Print the original list.
  3. Initialize a variable called “K” with a value.
  4. Create an empty list called “res” to store the sublists that meet the condition.
  5. Iterate through each sublist in “test_list”.
  6. For each sublist, iterate through each element.
  7. 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.
  8. 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.
  9. After iterating through all the sublists, print the sublists in “res” that satisfy the condition.

Python3




# Python3 code to demonstrate working of
# Access element at Kth index in String
# Using for loop
 
# initializing string list
test_list = [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
res = []
 
# iterate through each sublist
for sub in test_list:
    # check if all elements in the sublist are divisible by K
    flag = True
    for ele in sub:
        if ele % K != 0:
            flag = False
            break
    if flag:
        res.append(sub)
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads