0

I'm trying to get from: list = ['a', 'b', 'c', 'd']

to:

combinations = [['a','b'], ['a','c'], ['a','d'],
                ['b','c'], ['b','d'],
                ['c','d']]

(formatting just for readability in stackoverflow)

So, the cartesian product, all combinations, distinct regardless of order, and no ['a','a'] responses.

I've tried using zip ( tuple(zip(list*4)) ) and itertools (both permutations and product list(itertools.product(list, list))) but am struggling to clean it up. Itertools kind of works but I'm struggling to remove the dupes.

Is there a cleaner way? If not, please can someone help me out with the itertools response

a-joyce
  • 177
  • 1
  • 3
  • 9
  • 3
    `itertools.combinations(['a', 'b', 'c', 'd'], r=2)` – azro Feb 28 '23 at 18:45
  • 2
    The reason that a cartesian product isn't working for you, is that **the result you want is not a cartesian product**. This is a rare case, really. People **constantly** ask about "combinations" when they actually **do** want a Cartesian product; I can't remember the last time I saw the opposite. – Karl Knechtel Feb 28 '23 at 19:32
  • Well now I do feel special. Thanks azro & Karl ! – a-joyce Mar 01 '23 at 09:20

0 Answers0