title: MATLAB-style find() function in Python
body: <p>In MATLAB it is easy to find the indices of values that meet a particular condition:</p>
  
  <pre><code>&gt;&gt; a = [1,2,3,1,2,3,1,2,3];
  &gt;&gt; find(a &gt; 2)     % find the indecies where this condition is true
  [3, 6, 9]          % (MATLAB uses 1-based indexing)
  &gt;&gt; a(find(a &gt; 2))  % get the values at those locations
  [3, 3, 3]
  </code></pre>
  
  <p>What would be the best way to do this in Python?</p>
  
  <p>So far, I have come up with the following.  To just get the values:</p>
  
  <pre><code>&gt;&gt;&gt; a = [1,2,3,1,2,3,1,2,3]
  &gt;&gt;&gt; [val for val in a if val &gt; 2]
  [3, 3, 3]
  </code></pre>
  
  <p>But if I want the index of each of those values it's a bit more complicated:</p>
  
  <pre><code>&gt;&gt;&gt; a = [1,2,3,1,2,3,1,2,3]
  &gt;&gt;&gt; inds = [i for (i, val) in enumerate(a) if val &gt; 2]
  &gt;&gt;&gt; inds
  [2, 5, 8]
  &gt;&gt;&gt; [val for (i, val) in enumerate(a) if i in inds]
  [3, 3, 3]
  </code></pre>
  
  <p>Is there a better way to do this in Python, especially for arbitrary conditions (not just 'val > 2')?</p>
  
  <p>I found functions equivalent to MATLAB 'find' in NumPy but I currently do not have access to those libraries.</p>