Python - Filter rows with Elements as Multiple of K
Last Updated :
02 Jun, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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
# 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.
OutputThe 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
# 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))
OutputThe 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).
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 - 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 - 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 rows with required elements
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,
6 min read
Python - Filter rows with only Alphabets from List of Lists
Given Matrix, write a Python program to extract rows which only contains alphabets in its strings. Examples: Input : test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"], ["Gfg is good"], ["love", "gfg"]] Output : [['gfg', 'is', 'best'], ['love', 'gfg']] Explanation : All strings with just al
5 min read
Python - Elements with factors count less than K
Given a List of elements, get all elements with factors less than K. Examples: Input : test_list = [60, 12, 100, 17, 18, 19]. K = 4 Output : [17, 19] Explanation : Both elements have 2 factors. Input : test_list = [60, 12, 100, 360, 18, 900]. K = 4 Output : [] Explanation : All have greater than 4 f
4 min read
Python - Remove Elements in K distance with N
Given a list, remove all elements which are within K distance with N. Input : test_list = [4, 5, 9, 1, 10, 6, 13 ], K = 3, N = 5 Output : [9, 1, 10, 13] Explanation : 4 is removed as 5 - 4 = 1 < 3, hence its within distance. Input : test_list = [1, 10, 6, 13 ], K = 3, N = 5 Output : [1, 10, 13] E
5 min read
Python Program that filters out non-empty rows of a matrix
Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp
4 min read
Python - List Elements with given digit
Given list of elements and a digit K, extract all the numbers which contain K digit. Input : test_list = [56, 72, 875, 9, 173], K = 5 Output : [56, 875] Explanation : 56 and 875 has "5" as digit, hence extracted. Input : test_list = [56, 72, 875, 9, 173], K = 4 Output : [] Explanation : No number ha
6 min read
Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements. Examples: Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]Explanation: frequency of 4-> 4 which is even frequency
5 min read