Python – Extract Row with any Boolean True

PythonServer Side ProgrammingProgramming

When it is required to extract row with any Boolean True, a list comprehension is used along with the ‘any’ operator.

Below is a demonstration of the same −


 Live Demo

my_tuple = [[False, True], [False, False], [True, False, True], [False]]

print("The tuple is :")

my_result = [row for row in my_tuple if any(element for element in row)]

print("The result is ")


The tuple is :
[[False, True], [False, False], [True, False, True], [False]]
The result is
[[False, True], [True, False, True]]


  • A list of list is defined and displayed on the console.

  • A list comprehension is used to check if any element is present in the list.

  • The ‘any’ operator gives a True or False result.

  • This is converted to a list and is assigned to a variable.

  • This is the output that is displayed on the console.

Updated on 06-Sep-2021 08:13:16