2

I have an itertools.product of three sets that looks like this:

input:

t1 = set(['R'])
t2 = set(['Y'])
t3 = set(['A', 'C', 'H', 'M', 'T', 'W', 'Y'])

list(itertools.product(t1, t2, t3))

output:

[('R', 'Y', 'A'), ('R', 'Y', 'C'), ('R', 'Y', 'H'), ('R', 'Y', 'M'), ('R', 'Y', 'T'), ('R', 'Y', 'W'), ('R', 'Y', 'Y')]

But if I try and flatten it, it makes one long list, but what I want is this:

[('RYA'), ('RYC'), ('RYH'), ('RYM'), ('RYT'), ('RYW'), ('RYY')]

Any thoughts?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
dk09
  • 87
  • 1
  • 1
  • 7
  • 1
    sure, it doesnt really matter. Its more a question of the flattening of the product list. – dk09 Mar 23 '18 at 17:31
  • This is not a question about `itertools` or about Cartesian products, because the code that creates the list works properly, and because the question would be the same no matter how that list was created. Removed those tags. – Karl Knechtel Jun 25 '22 at 01:10

1 Answers1

8

Join each tuple in a list comprehension:

>>> [''.join(p) for p in itertools.product(t1, t2, t3)]
['RYH', 'RYA', 'RYC', 'RYY', 'RYT', 'RYM', 'RYW']
wim
  • 338,267
  • 99
  • 616
  • 750