You could use a list comprehension to add the elements from both lists zipped together, and use itertools.cycle
so that the iterator a
repeats itself as many times as necessary until b
is exhausted:
from itertools import cycle
a = [50, 17, 54, 26]
b = [19, 7, 8, 18, 36, 8, 18, 36, 18, 14]
[i+j for i,j in zip(cycle(a), b)]
Output
[69, 24, 62, 44, 86, 25, 72, 62, 68, 31]
Details
If you take a look at the iterator of tuples generated from the zipped expression:
list(zip(cycle(a),b))
[(50, 19),
(17, 7),
(54, 8),
(26, 18),
(50, 36),
(17, 8),
(54, 18),
(26, 36),
(50, 18),
(17, 14)]
You can see that the elements in a
cycle around until the other iterator is exhausted, making it very easy to perform some operation on the interleaved elements.