Suppose I have a list die_faces = [1, 2, 3, 4, 5, 6]
. I want to generate all 36 possible results for rolling two dice: (1, 1)
, (1, 2)
, (2, 1)
etc. If I try using permutations
from the itertools
standard library:
>>> import itertools
>>> die_faces = [1, 2, 3, 4, 5, 6]
>>> list(itertools.permutations(die_faces, 2))
[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5)]
there are only 30 results, missing the ones where the same number comes up on both dice. It seems that it only generates permutations without repetitions. How can I fix this?
list1_permutations = itertools.permutations(list1, len(list2))
- example here – Unstickpairs = itertools.permutations(_faces, 2); print(list(pairs))
– Unstick