Combining multiple sets of letters in Python
I am trying to figure out how to print all combinations for multiple sets of letters without repeating.
Example: A, B, C and X, Y, Z
Combinations:
AX AY Arizona BX FROM BZ CX CY CZ
+3
Ethan Barrett
source
to share
2 answers
You can use itertools.product
it to get what you want.
from itertools import product
a = ['A', 'B', 'C']
b = ['X', 'Y', 'Z']
for i in product(a, b):
print ''.join(i)
+8
Makoto
source
to share
You can just loop over both sets:
for a in abcstring:
for x in xyzstring:
print a + x
0
Explosion pills
source
to share