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 – 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