1all_combinations = [list(zip(each_permutation, list2)) for each_permutation in itertools.permutations(list1, len(list2))]
1def permutations(iterable, r=None):
2 # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
3 # permutations(range(3)) --> 012 021 102 120 201 210
4 pool = tuple(iterable)
5 n = len(pool)
6 r = n if r is None else r
7 if r > n:
8 return
9 indices = list(range(n))
10 cycles = range(n, n-r, -1)
11 yield tuple(pool[i] for i in indices[:r])
12 while n:
13 for i in reversed(range(r)):
14 cycles[i] -= 1
15 if cycles[i] == 0:
16 indices[i:] = indices[i+1:] + indices[i:i+1]
17 cycles[i] = n - i
18 else:
19 j = cycles[i]
20 indices[i], indices[-j] = indices[-j], indices[i]
21 yield tuple(pool[i] for i in indices[:r])
22 break
23 else:
24 return
25