Python: Various Lists To Cartesian Product, Variable Length
Question: I want to create words or rather combinations from three different arrays. The
output should be a Cartesian product with itself and the other array with
variable length. Example:

    
    
    array1 = ["a", "b", "c"]
    array2 = ["x", "y", "z"]
    Cartesian product of both, length 3: aaa, aab, aac ... axb, axc, axx... zzx, zzy, zzz
    

Could you point me in the right direction?


Answer: There is the `itertools` module which has method
[`product`](http://docs.python.org/2/library/itertools.html#itertools.product)
which does what you need.

    
    
    >>> from itertools import product
    >>> [''.join(items) for items in product(array1 + array2, repeat=3)]
    ['aaa', 'aab', 'aac', 'aax', 'aay', 'aaz', 'aba', 'abb', 'abc', 'abx', 'aby', 'abz', 'aca', 'acb', 'acc', 'acx', 'acy', 'acz', 'axa', 'axb', 'axc', 'axx', 'axy', 'axz', 'aya', 'ayb', 'ayc', 'ayx', 'ayy', 'ayz', 'aza', 'azb', 'azc', 'azx', 'azy', 'azz', 'baa', 'bab', 'bac', 'bax', 'bay', 'baz', 'bba', 'bbb', 'bbc', 'bbx', 'bby', 'bbz', 'bca', 'bcb', 'bcc', 'bcx', 'bcy', 'bcz', 'bxa', 'bxb', 'bxc', 'bxx', 'bxy', 'bxz', 'bya', 'byb', 'byc', 'byx', 'byy', 'byz', 'bza', 'bzb', 'bzc', 'bzx', 'bzy', 'bzz', 'caa', 'cab', 'cac', 'cax', 'cay', 'caz', 'cba', 'cbb', 'cbc', 'cbx', 'cby', 'cbz', 'cca', 'ccb', 'ccc', 'ccx', 'ccy', 'ccz', 'cxa', 'cxb', 'cxc', 'cxx', 'cxy', 'cxz', 'cya', 'cyb', 'cyc', 'cyx', 'cyy', 'cyz', 'cza', 'czb', 'czc', 'czx', 'czy', 'czz', 'xaa', 'xab', 'xac', 'xax', 'xay', 'xaz', 'xba', 'xbb', 'xbc', 'xbx', 'xby', 'xbz', 'xca', 'xcb', 'xcc', 'xcx', 'xcy', 'xcz', 'xxa', 'xxb', 'xxc', 'xxx', 'xxy', 'xxz', 'xya', 'xyb', 'xyc', 'xyx', 'xyy', 'xyz', 'xza', 'xzb', 'xzc', 'xzx', 'xzy', 'xzz', 'yaa', 'yab', 'yac', 'yax', 'yay', 'yaz', 'yba', 'ybb', 'ybc', 'ybx', 'yby', 'ybz', 'yca', 'ycb', 'ycc', 'ycx', 'ycy', 'ycz', 'yxa', 'yxb', 'yxc', 'yxx', 'yxy', 'yxz', 'yya', 'yyb', 'yyc', 'yyx', 'yyy', 'yyz', 'yza', 'yzb', 'yzc', 'yzx', 'yzy', 'yzz', 'zaa', 'zab', 'zac', 'zax', 'zay', 'zaz', 'zba', 'zbb', 'zbc', 'zbx', 'zby', 'zbz', 'zca', 'zcb', 'zcc', 'zcx', 'zcy', 'zcz', 'zxa', 'zxb', 'zxc', 'zxx', 'zxy', 'zxz', 'zya', 'zyb', 'zyc', 'zyx', 'zyy', 'zyz', 'zza', 'zzb', 'zzc', 'zzx', 'zzy', 'zzz']
    

