Cartesian Product of two lists (itertools)

Mark Wooding mdw at distorted.org.uk
Mon Jan 26 02:57:39 CET 2009


Thorsten Kampe <thorsten at thorstenkampe.de> writes:

> [((1, 4), 7), ((1, 4), 8), ((1, 5), 7), ((1, 5), 8), ((2, 4), 7), ((2, 
> 4), 8), ((2, 5), 7), ((2, 5), 8)]

[...]

> What's the best way to pre-process the arguments to "itertools.product" 
> or to post-process the result of "itertools.product" to get what I 
> want?!

Python has powerful destructuring capabilities:

        [(x, y, z) for ((x, y), z) in lopsided_list]

This seems simpler and is probably faster than a generalized flatten for
this size of problem, but I wouldn't use it to rearrange tuples with
more than four or five elements.

-- [mdw]



More information about the Python-list mailing list