Harrison Harrison - 1 year ago 79
Python Question

Is there a way to insert into elements of List2 alternately from elements of List1 quickly?

Say there is two lists.

List1 = [1,3,5,7,9]
List2 = [2,4,6,8,10]

I want to print every possibilities with no repeats that meet the pattern below:

Res = [odd,even,odd,even,odd,even,odd,even,odd,even]

For example,

Res = [1,2,3,4,5,6,7,8,9,10]
Res = [1,2,5,4,3,6,9,8,7,10]
Res = [3,2,1,4,9,6,5,10,7,8]

I tried to use itertools.product but I didn't know how to iterate by it.

Answer Source
>>> List1 = [1,3,5,7,9]
>>> List2 = [2,4,6,8,10]
>>> import itertools
>>> list(map(lambda x:[item for sublist in list(zip(*x)) for item in sublist], (itertools.product(itertools.permutations(List1), itertools.permutations(List2)))))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download