Why Numpy error when more than 1 equality in a line?

Why does this

import numpy as np

np.array([2,2]) == np.array([2,2]) == np.array([2,2])

give ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() while one equality check is fine.

np.array([2,2]) == np.array([2,2])

Output: array([ True, True])

Assuming np.array([True,True]) was the intermediate output somewhere (I don’t know where, both equality have equal precedence and no concept of associativity?) in the first code block, why does this, or the reverse direction not error then?

np.array([2,2]) == np.array([ True,  True])

Output: array([False, False])

Hi hanqi,

Basically, Python doesn’t know how to give you a single True/False result from this.
Your best option would be to compare them two at a time. You could use a loop, but I prefer this:

>     arr1=np.array([2,2])
>     arr2=np.array([2,2])
>     arr3=np.array([2,2])
>     print(np.logical_and( (arr1==arr2).all(), (arr2==arr3).all() ))