Feb-08-2023, 09:08 PM
Something like this?
https://pypi.org/project/more-itertools/
from itertools import zip_longest, chain def interleave(a, b): return [x for x in chain(*zip_longest(a, b)) if x is not None] a = [0, 0, 0, 0, 0] b = [1, 1, 1, 1] print(interleave(a, b)) print(interleave(b, a))
Output:[0, 1, 0, 1, 0, 1, 0, 1, 0]
[1, 0, 1, 0, 1, 0, 1, 0, 0]
There are functions named interleave and interleave_longest in more_itertools. This is not a standard library. You can read about it here:https://pypi.org/project/more-itertools/