Assuming la
is longer than lb
:
>>> import itertools
>>> [x+'_'+y for x,y in zip(la, itertools.cycle(lb))]
['a1_b1', 'a2_b2', 'a3_b1', 'a4_b2']
-
itertools.cycle(lb)
returns a cyclic iterator for the elements inlb
. -
zip(...)
returns a list of tuples in which each element corresponds to an element inla
coupled with the matching element in the iterator.