I realize you asked for a recursive solution, but is that really necessary?
>>> from itertools import product
>>> list(product([1, 2], repeat=3))
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]
Or if you really do need lists inside:
>>> from itertools import product
>>> map(list, product([1, 2], repeat=3))
[[1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2]]
And just in case you don't really need the whole thing as a list but just wants to process the triples, product
alone does the job:
>>> from itertools import product
>>> for triple in product([1, 2], repeat=3):
print(triple)
(1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 2, 2)
(2, 1, 1)
(2, 1, 2)
(2, 2, 1)
(2, 2, 2)
Recursive solution:
def perm(elements, n):
return [[e] + p for p in perm(elements, n-1) for e in elements] if n else [[]]
print(perm([1, 2], 3))