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
  • 13,160
  • 8
  • 44
  • 80

2 Answers2

0

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
  • 77,035
  • 9
  • 115
  • 152
0

Since you are looking for a functional way:

from operator import add
reduce(add,zip(a,b),[])
Uri Goren
  • 12,532
  • 6
  • 50
  • 100