Python AND operator on two boolean lists - how?
and
simply returns either the first or the second operand, based on their truth value. If the first operand is considered false, it is returned, otherwise the other operand is returned.
Lists are considered true when not empty, so both lists are considered true. Their contents don't play a role here.
Because both lists are not empty, x and y
simply returns the second list object; only if x
was empty would it be returned instead:
>>> [True, False] and ['foo', 'bar']
['foo', 'bar']
>>> [] and ['foo', 'bar']
[]
See the Truth value testing section in the Python documentation:(emphasis mine), and the Boolean operations section right below that:Any object can be tested for truth value, for use in an
if
orwhile
condition or as operand of the Boolean operations below. The following values are considered false:[...]
- any empty sequence, for example,
''
,()
,[]
.
[...]
All other values are considered true — so objects of many types are always true.
You indeed need to test the values contained in the lists explicitly. You can do so with a list comprehension, as you discovered. You can rewrite it with the
x and y
if x is false, then x, else yThis is a short-circuit operator, so it only evaluates the second argument if the first one is
True
.
zip()
function to pair up the values:[a and b for a, b in zip(x, y)]
Logical operation between two Boolean lists
Your lists aren't comparing each individual value, they're comparing the existence of values in the list.
For any truthy variables a
and b
:
a and b
> b #The program evaluates a, a is truthy, it evaluates b, b is truthy, so it returns the last evaluated value, b.
a or b
> a #The program evaluates a, a is truthy, so the or statement is true, so it returns the last evaluated value, a.
Now, truthy depends on the type. For example, integers are truthy for my_int != 0
, and are falsy for my_int == 0
. So if you have:a = 0
b = 1
a or b
> b #The program evaluates a, a is falsy, so the or statement goes on to evaluate b, b is truthy, so the or statement is true and it returns the last evaluated value b.
Python ```and``` two bool list?
Because Python doesn't do element-wise vector operations like that. The way the normal and
operator works is, if the first operand has a "true" value, then the result is the second value. Your first list is not empty, so it's true, so it returned the entire second list.
The numpy
module does element-wise operations like that, but not straight Python.
Operator with two Boolean in python
You need to do this:
Replace &
with and
:
In [638]: bool(re.search(r'\d', "4foo")) and len("4foo")==4
Out[638]: True
and
tests whether both expressions are logically True while &
(when used with True/False values) tests if both are True. Logical AND operation across multiple lists
One way would be to use zip
to get all corresponding elements and then to ask if they are all
true:
map(all, zip(*d.values()))
Result it: [True, False]
Compare two boolean lists using Python
Use the below method to compare two lists and get the boolean list of results. The below answer will work even if the two lists are not of the same size. Use itertools.zip_longest
for Python 3.x
import itertools
alist = [1,2,3,4]
blist = [3,2,5]
compare_list = [(a == b) for a,b in itertools.izip_longest(alist, blist)]
print compare_list
Output:[False, True, False, False]
Are there builtin functions for elementwise boolean operators over boolean lists?
There is not a built-in way to do this. Generally speaking, list comprehensions and the like are how you do elementwise operations in Python.
Numpy does provide this (using &
, for technical limitations) in its array type. Numpy arrays usually perform operations elementwise.
Related Topics
Conditional with Statement in Python
Define CSS Class in Django Forms
Representing and Solving a Maze Given an Image
Building a Minimal Plugin Architecture in Python
First Python List Index Greater Than X
Python Running as Windows Service: Oserror: [Winerror 6] the Handle Is Invalid
How Does the Max() Function Work on List of Strings in Python
How to Log a Python Error with Debug Information
Django Abstract Models Versus Regular Inheritance
Matplotlib: How to Show a Figure That Has Been Closed
Memoization Library for Python 2.7
Unexpected Results Converting Timezones in Python