Idiomatic way of repeating items in a sequence.

Batista, Facundo FBatista at uniFON.com.ar
Mon Jun 30 17:53:01 CEST 2003


#- def repeatitems(L, n):
#-     out = []
#-     for i in L:
#-         out.extend([i]*n)
#-     return out
#- 
#- Sometimes a for loop is better :-)

Whitout a lambda and without a for:

def repeatitems(L, n):
	return [x for x in L*n]

CAUTION: the resulting list is not in the same order of your example:

>>> repeatitems(['a', 'b', 'c'], 3)
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']





More information about the Python-list mailing list