I use itertools (especially cycle, repeat, chain) to make python behave more like R and in other functional / vector applications. Often this lets me avoid the overhead and complication of Numpy.
# in R, shorter iterables are automatically cycled
# and all functions "apply" in a "map"-like way over lists
> 0:10 + 0:2
[1] 0 2 4 3 5 7 6 8 10 9 11
Python
#Normal python
In [1]: range(10) + range(3)
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
## this code is terrible, but it demos the idea.
from itertools import cycle
def addR(L1,L2):
n = max( len(L1), len(L2))
out = [None,]*n
gen1,gen2 = cycle(L1), cycle(L2)
ii = 0
while ii < n:
out[ii] = gen1.next() + gen2.next()
ii += 1
return out
In [21]: addR(range(10), range(3))
Out[21]: [0, 2, 4, 3, 5, 7, 6, 8, 10, 9]