List multiplication

cartesian-product, list, python

Solution

The `itertools` module contains a number of helpful functions for this sort of thing. It looks like you may be looking for `product`:

>>> import itertools
>>> L = [1,2,3]
>>> itertools.product(L,L)
<itertools.product object at 0x83788>
>>> list(_)
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

Problem

I have a list L = [a, b, c] and I want to generate a list of tuples : ``` [(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...] ``` I tried doing L * L but it didn't work. Can someone tell me how to get this in python.

Original source

Related problems