ABC = [ sum(z, []) for z in itertools.product(*SETS) ]
product(*SETS)
basically does product(A, B, C)
. The technical term is argument unpacking.
sum(z, [])
basically does a + b + c + []
.
EDIT:
As smart people said in the comments sum
isn't a best way to join a list of lists. O(n^2) time complexity is pretty brutal.
To quote a documentation:
For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling ''.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain().
That's better:
from itertools import chain, product
ABC = [ list(chain(*z)) for z in product(*SETS) ]
or, if two argument unpackings is one argument unpacking too many:
ABC = [ list(chain.from_iterable(z)) for z in product(*SETS) ]
or, if you're into map
:
ABC = map(list, map(chain.from_iterable, product(*SETS)))