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


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


source


You can just loop over both sets:



for a in abcstring:
    for x in xyzstring:
        print a + x

      

0


source







All Articles