Velocity Reviews > Idiomatic way of repeating items in a sequence.

# Idiomatic way of repeating items in a sequence.

alr
Guest
Posts: n/a

 06-30-2003
I need to repeat each item in a list n times, like this function does:

def repeatitems(sequence, repetitions):
newlist = []
for item in sequence:
for i in range(repetitions):
newlist.append(item)
return newlist

Output:

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

['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']

Clear and simple. But i wonder if there is a more idiomatic way. Surely not this:

def repeatitems(sequence, repetitions):
return reduce(lambda l, i: l + i, [[item] * repetitions for item in sequence])

?

Max M
Guest
Posts: n/a

 06-30-2003
alr wrote:
> I need to repeat each item in a list n times, like this function does:
>
> def repeatitems(sequence, repetitions):
> newlist = []
> for item in sequence:
> for i in range(repetitions):
> newlist.append(item)
> return newlist

I would make just a minor change:

def repeatitems(sequence, repetitions):
newlist = []
for item in sequence:
newlist += repetitions*[item]
return newlist

regards Max M

Duncan Booth
Guest
Posts: n/a

 06-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (alr) wrote in
news:(E-Mail Removed) m:

> I need to repeat each item in a list n times, like this function does:
>
> def repeatitems(sequence, repetitions):
> newlist = []
> for item in sequence:
> for i in range(repetitions):
> newlist.append(item)
> return newlist
>
> Output:
>
> >>> repeatitems(['a', 'b', 'c'], 3)

> ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
>
> Clear and simple. But i wonder if there is a more idiomatic way.

The most obvious one that springs to mind is just a slight simplification

def repeatitems(sequence, repetitions):
newlist = []
for item in sequence:
newlist.extend([item] * repetitions)
return newlist

--
Duncan Booth (E-Mail Removed)
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

Peter Otten
Guest
Posts: n/a

 06-30-2003
alr wrote:

> I need to repeat each item in a list n times, like this function does:
>
> def repeatitems(sequence, repetitions):
> newlist = []
> for item in sequence:
> for i in range(repetitions):
> newlist.append(item)
> return newlist
>

....
> But i wonder if there is a more idiomatic way.

def repeatItems(sequence, repetitions):
return [[item]*repetitions for item in sequence]

Richard Brodie
Guest
Posts: n/a

 06-30-2003

"Peter Otten" <(E-Mail Removed)> wrote in message news:bdpdd7\$iue\$00\$(E-Mail Removed)-online.com...

>
> def repeatItems(sequence, repetitions):
> return [[item]*repetitions for item in sequence]

Unfortunately that is equivalent to:

def repeatitems(sequence, repetitions):
newlist = []
for item in sequence:
newlist.append([item] * repetitions)
return newlist

and not:

def repeatitems(sequence, repetitions):
newlist = []
for item in sequence:
newlist.extend([item] * repetitions)
return newlist

Duncan Booth
Guest
Posts: n/a

 06-30-2003
Duncan Booth <(E-Mail Removed)> wrote in
news:Xns93AA81EC76E7Eduncanrcpcouk@127.0.0.1:

> The most obvious one that springs to mind is just a slight
>
> def repeatitems(sequence, repetitions):
> newlist = []
> for item in sequence:
> newlist.extend([item] * repetitions)
> return newlist
>

Or, if you are in a "I've got a new toy to play with" mood you could use
itertools from Python 2.3 to obfuscate it somewhat:

from itertools import chain, izip, repeat
def repeatiterate(sequence, repetitions):
return chain(*izip(*repeat(sequence, repetitions)))

This version returns an iterator, so you might want to throw in a call to
'list' if you want to do anything other than iterating over the result.

--
Duncan Booth (E-Mail Removed)
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

Peter Otten
Guest
Posts: n/a

 06-30-2003
Richard Brodie wrote:

> Unfortunately that is equivalent to:
>
> def repeatitems(sequence, repetitions):
> newlist = []
> for item in sequence:
> newlist.append([item] * repetitions)
> return newlist
>

.... but it looked so good. I should have tested it, though.
Je suis desole

Terry Reedy
Guest
Posts: n/a

 06-30-2003

"alr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> I need to repeat each item in a list n times, like this function

does:

Is this really the right question? Any code that requires such an
n-repeat list *could* be rewritten (if you own it) to use the replist
in condensed form: (items, n). If this is not possible, one could
also define a replist class with a __getitem__(self, index) that
divides index by self.n and an __iter__() that returns an appropriate
generator.

Terry J. Reedy

John Hunter
Guest
Posts: n/a

 06-30-2003
>>>>> "alr" == alr <(E-Mail Removed)> writes:

alr> reduce(lambda l, i: l + i, [[item] * repetitions for item in

This doesn't look too bad to me, but perhaps list comprehensions are
clearer?

seq = ['a', 'b', 'c']
print [x for x in seq for x in seq]

John Hunter
Guest
Posts: n/a

 06-30-2003
>>>>> "alr" == alr <(E-Mail Removed)> writes:

alr> reduce(lambda l, i: l + i, [[item] * repetitions for item in
alr> sequence])

Oops, premature hit of send key. What I meant to say was

seq = ['a', 'b', 'c']
print [x for x in seq for i in range(len(seq))]

JDH