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>

<pre><code>import itertools

x = [1,2,3,4]

for each in itertools.permutations(x,2):
    print(each)
</code></pre>

<p>Note that itertools is a generator object, meaning you need to iterate through it to get all you want. The '2' is optional, but it tells the function whats the number per combonation you want.  </p>

<p><a href="https://docs.python.org/3/library/itertools.html#itertools.permutations" rel="nofollow">You can read more here</a></p>

<p>Edited:</p>

<p>As ForceBru said in the comment, you can unpack the generator to print, skipping the for loop together But I would still iterate through it as you might not know how big the generated object will be:</p>

<pre><code>print(*itertools.permutations(x, 2))
</code></pre>

python arrays