a = [0, 2, 4]
b = list(map(lambda x: x+1, a))
merged list c = [0, 1, 2, 3, 4, 5]
c = [a[0], b[0], a[1], b[1] ... ]
Can I achieve the result with functional programming ? Instead of just looping ?
Thanks in advance
a = [0, 2, 4]
b = list(map(lambda x: x+1, a))
merged list c = [0, 1, 2, 3, 4, 5]
c = [a[0], b[0], a[1], b[1] ... ]
Can I achieve the result with functional programming ? Instead of just looping ?
Thanks in advance
Sure, there are many ways. Here is a straightforward list-comprehension:
>>> a = [0, 2, 4]
>>> b = [1, 3, 5]
>>> [p for pair in zip(a,b) for p in pair]
[0, 1, 2, 3, 4, 5]
>>>
Or if you would prefer to use itertools
>>> import itertools
>>> list(itertools.chain.from_iterable(zip(a,b)))
[0, 1, 2, 3, 4, 5]
Since you are looking for a functional way:
from operator import add
reduce(add,zip(a,b),[])