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 program to fetch the indices of true values in a Boolean list
- Python - False indices in a boolean list
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python | Contiguous Boolean Range
- Python | Ways to concatenate boolean to string
- Python | Ways to convert Boolean values to integer
- Python - Test Boolean Value of Dictionary
- Understanding Boolean Logic in Python 3
- Python - Convert String Truth values to Boolean
- Python EasyGUI – Boolean Box
- Python - Extract Row with any Boolean True
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
- Boolean Indexing in Pandas
- Scala short ==(x: Int): Boolean
- Boolean Operators - Django Template Tags
- Boolean Fields in Serializers - Django REST Framework
- Python | Convert list of string to list of list
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.