0
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

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86

2 Answers2

1

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]
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
0

Since you are looking for a functional way:

from operator import add
reduce(add,zip(a,b),[])
Uri Goren
  • 13,386
  • 6
  • 58
  • 110