Check if variable is None or numpy.array
<p>I look up in a table if keys have associated arrays, or not. By design, my <code>table.__getitem__()</code> somtimes returns <code>None</code> rather than <code>KeyError</code>-s.  I would like this value to be either <code>None</code>, or the numpy array associated with <code>w</code>.</p>

<pre><code>value = table[w] or table[w.lower()]
# value should be a numpy array, or None
if value is not None:
    stack = np.vstack((stack, value))
</code></pre>

<p>Only if I go with the above code, and the first lookup is a match, I get :</p>

<pre><code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
</code></pre>

<p>and if I go with <code>value = table[w].any() or table[w.lower()].any()</code>, then if it's a mismatch, I expectedly bump into :</p>

<pre><code>AttributeError: 'NoneType' object has no attribute 'any'
</code></pre>

<p>I must be missing the correct way to do this, how to do ?</p>

<p>IIUC this should work:</p>

<pre><code>value = table[w]
if value is None:
    value = table[w.lower()]
</code></pre>

python numpy