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 - Extract rows with Even length strings
In this article, we have a given Matrix, and extract rows that are of even lengths. Input : test_list = [["gfg", "is", "best"], ["best", "good", "geek"], ["is", "better"], ["for", "cs"]] Output : [['best', 'good', 'geek'], ['is', 'better']] Explanation : All strings are of even length.Input : test_l
8 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 | Count true booleans in a list
Given a list of booleans, write a Python program to find the count of true booleans in the given list. Examples: Input : [True, False, True, True, False] Output : 3 Input : [False, True, False, True] Output : 2 Method #1: Using List comprehension One simple method to count True booleans in a list i
3 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 - 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
Ways to concatenate boolean to string - Python
Concatenating a boolean to a string in Python involves converting the boolean value (True or False) into a string format and then combining it with other string elements. For example, given s = "Facts are" and v = True, the expected output after concatenation is "Facts are True". Let's explore diffe
2 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
Copy Rows and Columns in Excel Using Python
Manipulating data in Excel often involves tasks such as copying specific rows or columns based on certain conditions. Python, with its powerful libraries like Pandas and openpyxl, provides efficient solutions for handling Excel files. In this article, we will explore how to copy rows and columns in
3 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