- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Extract Row with any Boolean True
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 −
Example
my_tuple = [[False, True], [False, False], [True, False, True], [False]] print("The tuple is :") print(my_tuple) my_result = [row for row in my_tuple if any(element for element in row)] print("The result is ") print(my_result)
Output
The tuple is : [[False, True], [False, False], [True, False, True], [False]] The result is [[False, True], [True, False, True]]
Explanation
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.
Advertisements