Logical operation between two Boolean lists
If what you actually wanted was element-wise boolean operations between your two lists, consider using the numpy
module:
>>> import numpy as np
>>> a = np.array([True, False, False])
>>> b = np.array([True, True, False])
>>> a & b
array([ True, False, False], dtype=bool)
>>> a | b
array([ True, True, False], dtype=bool)
Both lists are truthy because they are non-empty.
Both and
and or
return the operand that decided the operation's value.
If the left side of and
is truthy, then it must evaluate the right side, because it could be falsy, which would make the entire operation false (false and
anything is false). Therefore, it returns the right side.
If the left side of or
is truthy, it does not need to evaluate the right side, because it already knows that the expression is true (true or
anything is true). So it returns the left side.
If you wish to perform pairwise comparisons of items in the list, use a list comprehension, e.g.:
[x or y for (x, y) in zip(a, b)] # a and b are your lists
I think you need something like this:
[x and y for x, y in zip([True, False, False], [True, True, False])]
This is normal, because and
and or
actually evaluate to one of their operands. x and y
is like
def and(x, y):
if x:
return y
return x
while x or y
is like
def or(x, y):
if x:
return x
return y
Since both of your lists contain values, they are both "truthy" so and
evaluates to the second operand, and or
evaluates to the first.