Filter an array in Python with 2 conditions

arrays, list, numpy, python

Solution

You should use the bitwise (thanks @askewchan) version of the operator `and` which is `&`.

i.e.

 print A[(B > 5) & (B < 13)] 

Problem

How to filter an array A according to two conditions ? ``` A = array([1, 2.3, 4.3, 10, 23, 42, 23, 12, 1, 1]) B = array([1, 7, 21, 5, 9, 12, 14, 22, 12, 0]) print A[(B < 13)] # here we get all items A[i] with i such that B[i] < 13 print A[(B > 5) and (B < 13)] # here it doesn't work # how to get all items A[i] such that B[i] < 13 # AND B[i] > 5 ? ``` The Error is: ValueError: The truth value of an array with more than one element is ambiguous.

Original source