Idiomatic way of repeating items in a sequence.
bober at acm.cs.nyu.edu
Wed Jul 2 03:20:29 CEST 2003
On Mon, 2003-06-30 at 10:53, Batista, Facundo wrote:
> 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']
this code hurts so bad that i must respond...
L * n
is the same as
[x for x in L * n]
except that the latter iterates through L * n applying the identity
transformation, and is much much slower.
keep things simple.
More information about the Python-list