What is the fastest way to get all pairwise combinations in an array in python?
<p>For example, if the array is [1,2,3,4]
I want the output to be [1,2],[1,3],[1,4],[2,3],[2,4],[3,4]
I want a solution which is better than the brute force method of  using two for loops.</p>

<p>Though the previous answer will give you all pairwise orderings, the example expected result seems to imply that you want all <em>unordered</em> pairs. </p>

<p>This can be done with <code>itertools.combinations</code>:</p>

<pre><code>&gt;&gt;&gt; import itertools
&gt;&gt;&gt; x = [1,2,3,4]
&gt;&gt;&gt; list(itertools.combinations(x, 2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
</code></pre>

<p>Compare to the other result:</p>

<pre><code>&gt;&gt;&gt; list(itertools.permutations(x, 2))
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
</code></pre>

python arrays