Python merging two lists with all possible permutations
repeat
the first list, permutate
the second and zip
it all together
>>> from itertools import permutations, repeat>>> a = [1, 2, 3]>>> b = [4, 5, 6]>>> list(list(zip(r, p)) for (r, p) in zip(repeat(a), permutations(b)))[[(1, 4), (2, 5), (3, 6)], [(1, 4), (2, 6), (3, 5)], [(1, 5), (2, 4), (3, 6)], [(1, 5), (2, 6), (3, 4)], [(1, 6), (2, 4), (3, 5)], [(1, 6), (2, 5), (3, 4)]]
EDIT: As Peter Otten noted, the inner zip
and the repeat
are superfluous.
[list(zip(a, p)) for p in permutations(b)]