title: Finding all combinations of paired numpy arrays via meshgrid
body: <p>Suppose I have a numpy array that consists of pairs of values. I'd like to find all combinations of the pairs without tearing them apart. Particularly, I was hoping for a <code>numpy.meshgrid</code> solution for this.</p>
  
  <p>Imagine an array constructed like:</p>
  
  <pre><code>ab = np.array([[1,10], [2,20], [3,30], [4,40]])
  </code></pre>
  
  <p>Then my desired output is</p>
  
  <pre><code>&gt;&gt;&gt; out: ([1,10], [2,20])
           ([1,10], [3,30])
           ([1,10], [4,40])
           ([2,20], [3,30])
           ([2,20], [4,40])
           ([3,30], [4,40])
  </code></pre>
  
  <p>The output can be either a <code>np.array</code>, or a <code>tuple</code> (I can convert accordingly afterwards). Please notice how duplicates are omitted in my results, neglecting the order of my couples (if <code>[[1,10], [2,20]]</code> is already there, I don't want <code>[[2,20], [1,10]]</code> in my output). For the real case, <code>ab</code> is of size 30,000, so speed is another issue.</p>
  
  <p>That's why I tried meshgrid in the first place.
  For the simple case of single values, this is easily done (yet, still with the duplicates):</p>
  
  <pre><code>a = np.array([1,2,3,4])
  mesh = np.array(np.meshgrid(a,a)).T.reshape(-1,2)
  &gt;&gt;&gt; out: [[1 1]
            [1 2]
            [1 3]
            [1 4]
            [2 1]
            [...]
            [4 4]]
  </code></pre>
  
  <p>but for my pairs, my attempt of </p>
  
  <pre><code>mesh = np.array(np.meshgrid(ab,ab)).T
  </code></pre>
  
  <p>gives me </p>
  
  <pre><code>[[[ 1  1]
    [ 1 10]
    [ 1  2]
    [ 1 20]
    [ 1  3]
    [ 1 30]
    [ 1  4]
    [ 1 40]]
  
   [[10  1]
    [10 10]
    [10  2]
    [10 20]
  ...    
    [40  3]
    [40 30]
    [40  4]
    [40 40]]]
  </code></pre>
  
  <p>In other words: meshgrid breaks up my pairs. I assume the solution is near, but I couldn't come up with it on my own. Any help is appreciated, thanks!</p>