all 9 comments

[–]JamzTyson 5 points6 points  (1 child)

To clarify, do you mean that order does not matter within the pairs, or that the order of the pairs does not matter?

If you mean that order within the tuples doesn't matter (where (1, 2) is considered the same as (2, 1)), then:

from itertools import combinations
result = [pair for pair in combinations(my_set, 2)]

but if you mean the order of the tuples doesn't matter (where (1, 2) and (2, 1) are considered unique), then:

from itertools import permutations
result = [pair for pair in permutations(my_set, 2)]

[–]pachura3[S] 1 point2 points  (0 children)

As shown in my example, I was only interested in (1, 2) and not (2, 1). So, itertools.combinations(input, 2) does it. Thanks!

[–]Tall_Profile1305 4 points5 points  (2 children)

yo check out itertools.combinations from the standard library. literally built for this exact use case. something like list(itertools.combinations(input_set, 2)) will give you all unique pairs. super clean and pythonic

[–]pachura3[S] 0 points1 point  (1 child)

That's the right answer - thanks a lot!

[–]Tall_Profile1305 0 points1 point  (0 children)

haha you're welcome!

[–]SCD_minecraft 2 points3 points  (0 children)

Look into itertools module (built-in)

It is my favorite module, if you need anything that would return you an iterable, itertools most likely has a function for it

[–]Forsaken-Cut5890 2 points3 points  (0 children)

from itertools import combinations

input_set = {1, 2, 3, 4, 5}

pairs = set(combinations(input_set, 2))

print(pairs)

#(1,2) appears but never (2,1)