Python - Filter Rows with Range Elements
Last Updated :
22 Mar, 2023
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 : test_list = [[3, 2, 4, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5
Output : [[2, 3, 4, 5, 6, 7]]
Explanation : 2, 3, 4, 5 all are present in above rows.
Method #1 : Using all() + list comprehension
In this, we check for all the elements in range for presence using all() and list comprehension is used for the task of iteration of elements.
Python3
# Python3 code to demonstrate working of
# Filter Rows with Range Elements
# Using all() + list comprehension
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19],
[2, 5, 10], [2, 3, 4, 5, 6, 7]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 2, 5
# checking for presence of all elements using in operator
res = [sub for sub in test_list if all(ele in sub for ele in range(i, j + 1))]
# printing result
print("Extracted rows : " + str(res))
OutputThe original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + all()
In this, task of filtering is done using filter() and lambda function, all() is again used to ensure all elements presence in range.
Python3
# Python3 code to demonstrate working of
# Filter Rows with Range Elements
# Using filter() + lambda + all()
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19],
[2, 5, 10], [2, 3, 4, 5, 6, 7]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 2, 5
# filter() and lambda used to filter elements
res = list(filter(lambda sub: all(
ele in sub for ele in range(i, j + 1)), test_list))
# printing result
print("Extracted rows : " + str(res))
OutputThe original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. filter() + lambda + all() performs n*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 Range Elements
import itertools
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19],
[2, 5, 10], [2, 3, 4, 5, 6, 7]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 2, 5
res = list(itertools.filterfalse(lambda sub: not all(
ele in sub for ele in range(i, j + 1)), test_list))
# printing result
print("Extracted rows : " + str(res))
OutputThe original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #4:Using List Comprehension and Set Intersection:
Algorithm:
- Initialize the input list and range values.
- Use list comprehension to iterate through each sublist in the input list.
- Check if the set intersection of the sublist and the range values is equal to the range values set.
- If it is, append the sublist to the result list.
- Print the result list.
Python3
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
# printing original list
print("The original list is : " + str(test_list))
i, j = 2, 5
res = [lst for lst in test_list if set(range(i, j+1)).intersection(lst) == set(range(i, j+1))]
print("Extracted rows: ", res)
#This code is contributed by Jyothi pinjala
OutputThe original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows: [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]
Time complexity:
The time complexity of this code is O(nm), where n is the number of sublists in the input list and m is the average length of each sublist. This is because we are iterating through each sublist and performing set operations on them.
Space complexity:
The space complexity of this code is O(k), where k is the length of the range between i and j. This is because we are using a set to store the range values.
Similar Reads
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 - Extract tuples with elements in Range Given list of tuples, extract tuples having elements in range. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 5, 6 Output : [(5, 6)] Explanation : Only 1 tuple lies in range of 5-6. Input : test_list = [(4, 5, 7), (5, 6), (3, 8, 10 ), (4, 10)], strt, end = 1, 10 Output :
4 min read
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
6 min read
Python - Retain all K elements Rows Sometimes, while working with Python lists, we can have a problem in which we need to retain rows which have only K as elements. This kind of application can occur in data domains which take Matrix as input. Let's discuss certain ways in which this task can be performed. Input : test_list = [[7, 6],
8 min read
Python - Delete elements in range In Python, we often need to remove elements from a list within a specific range. This can be useful when cleaning data or modifying lists based on certain conditions. Using List Comprehension List comprehension is one of the most Pythonic and efficient ways to filter out elements. It is concise, eas
3 min read