Python - Extract Row with any Boolean True
Last Updated :
24 Apr, 2023
Given a Boolean Matrix, extract a row that contains at least one boolean True value.
Input : test_list = [[False, False], [True, True, True], [False, True], [False]]
Output : [[True, True, True], [False, True]]
Explanation : All rows with atleast 1 True extracted.
Input : test_list = [[False, False], [False]]
Output : []
Explanation : No rows with even one True.
Method #1 : Using list comprehension + any()
In this, we check for any element to be boolean true using any() and list comprehension is used for task of iteration of rows in matrix.
Python3
# Python3 code to demonstrate working of
# Extract Row with any Boolean True
# Using list comprehension + any()
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
# printing original list
print("The original list is : " + str(test_list))
# using any() to check for any True value
res = [sub for sub in test_list if any(ele for ele in sub)]
# printing result
print("Extracted Rows : " + str(res))
OutputThe original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]
Time Complexity : O(n^2)
Space Complexity : O(n)
Method #2: Using any() + filter() + lambda
In this, we perform the task of checking for any True value using any() and filter(), and lambda is used to filter out matching rows.
Python3
# Python3 code to demonstrate working of
# Extract Row with any Boolean True
# Using any() + filter() + lambda
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
# printing original list
print("The original list is : " + str(test_list))
# using any() to check for any True value
# filter() to perform filtering
res = list(filter(lambda sub: any(ele for ele in sub), test_list))
# printing result
print("Extracted Rows : " + str(res))
OutputThe original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]
Time Complexity : O(n^2)
Space Complexity : O(n)
Method #3 : Using count() method
Python3
# Python3 code to demonstrate working of
# Extract Row with any Boolean True
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
# printing original list
print("The original list is : " + str(test_list))
res=[]
for i in test_list:
if(i.count(True)>=1):
res.append(i)
# printing result
print("Extracted Rows : " + str(res))
OutputThe original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]
Time Complexity : O(n*m)
Space Complexity : O(n)
Method #4 : Using in operator
Python3
# Python3 code to demonstrate working of
# Extract Row with any Boolean True
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
# printing original list
print("The original list is : " + str(test_list))
# using any() to check for any True value
res=[]
for i in test_list:
if True in i:
res.append(i)
# printing result
print("Extracted Rows : " + str(res))
OutputThe original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]
time complexity : O(n * m)
space complexity : O(n)
Method #5:Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of
# Extract Row with any Boolean True
import itertools
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
# printing original list
print("The original list is : " + str(test_list))
res = list(itertools.filterfalse(lambda sub: not any(ele for ele in sub), test_list))
# printing result
print("Extracted Rows : " + str(res))
OutputThe original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]
Time Complexity:O(N*N)
Auxiliary Space:O(N*N)
Method 6 : using a for loop and a temporary list to keep track of the rows that contain True.
step-by-step approach :
Initialize an empty list res to store the rows that contain True.
Loop over each row i in the test_list.
Check if any element in the row i is True using the any() function. If it is, append the row i to the res list.
After looping over all rows, print the res list to show the extracted rows.
Python3
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
# printing original list
print("The original list is : " + str(test_list))
# using for loop and temporary list to extract rows with any True value
res = []
for i in test_list:
if any(i):
res.append(i)
# printing result
print("Extracted Rows : " + str(res))
OutputThe original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]
Time complexity: O(nm), where n is the number of rows and m is the maximum length of a row.
Auxiliary space: O(k), where k is the number of rows with at least one True value.
Similar Reads
Python - Extract rows with Complex data types Given Matrix, extract rows with complex data types. Examples: Input : test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]] Output : [[1, [3, 4], 9], [7, (2, 3), 3, 9]] Explanation : Rows have lists and tuples respectively. Input : test_list = [[4, 2, [5]], [1, [3, 4], 9], [5], [7, (2, 3),
5 min read
Python - Convert String Truth values to Boolean Converting string truth values like "True" and "False" into their corresponding boolean values in Python is a straightforward yet useful operation. Methods such as dictionaries, direct comparison, and built-in functions offer simple and efficient ways to achieve this transformation for various use c
2 min read
Python - Extract Particular data type rows Given A Matrix, extract all the rows which have all the elements with particular data type. Input : test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]], data_type = int Output : [[2, 6, 7], [9, 10, 11]] Explanation : All lists with integer are extracted. Input : test_list = [[4, 5
3 min read
Python - Remove rows with Numbers Given a Matrix, remove rows with integer instances. Input : test_list = [[4, 'Gfg', 'best'], ['gfg', 5, 'is', 'best'], [3, 5], ['GFG', 'Best']] Output : [['GFG', 'Best']] Explanation : All rows with numbers are removed. Input : test_list = [[4, 'Gfg', 'best'], ['gfg', 5, 'is', 'best'], [3, 5], ['GFG
7 min read
Python | Boolean List AND and OR operations Sometimes, while working with Python list, we can have a problem in which we have a Boolean list and we need to find Boolean AND or OR of all elements in it. This kind of problem has application in Data Science domain. Let's discuss an easy way to solve both these tasks. Method #1 : AND operation -
3 min read
Convert Python boolean values to strings Yes or No When you're using true or false values in Python, changing them to "Yes" or "No" strings can make your code easier to read. This article shows different ways to do it, giving you choices based on how you like to write your code. Here, we will convert the Python Boolean values to string as Yes or No
3 min read