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 – Using
The solution to this problem is quite straight forward, but application awareness is required. The all() performs the Boolean AND of the list and returns the result.
The original list is : [True, True, False, True, False] Result after performing AND among elements : False
Method #2 : OR operation – Using
This task can be performed using
any(). This checks for any True element in list and returns True in that case else returns a False.
The original list is : [True, True, False, True, False] Result after performing OR among elements : True
- Python | Filter list by Boolean list
- Python | Boolean list initialization
- Python - False indices in a boolean list
- Python program to fetch the indices of true values in a Boolean list
- Python | Contiguous Boolean Range
- Python | Ways to concatenate boolean to string
- Python | Ways to convert Boolean values to integer
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Operations on Python Counter
- Arithmetic operations using OpenCV | Python
- Python: Operations on Numpy Arrays
- Image segmentation using Morphological operations in Python
- Python | Intensity Transformation Operations on Images
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.