Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Idiomatic way of repeating items in a sequence. (http://www.velocityreviews.com/forums/t319016-idiomatic-way-of-repeating-items-in-a-sequence.html)

 alr 06-30-2003 11:26 AM

Idiomatic way of repeating items in a sequence.

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 06-30-2003 11:44 AM

Re: Idiomatic way of repeating items in a sequence.

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 06-30-2003 11:54 AM

Re: Idiomatic way of repeating items in a sequence.

anlri@wmdata.com (alr) wrote in

> 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 duncan@rcp.co.uk
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 06-30-2003 01:12 PM

Re: Idiomatic way of repeating items in a sequence.

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 06-30-2003 01:43 PM

Re: Idiomatic way of repeating items in a sequence.

"Peter Otten" <__peter__@web.de> wrote in message news:bdpdd7\$iue\$00\$1@news.t-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 06-30-2003 02:36 PM

Re: Idiomatic way of repeating items in a sequence.

Duncan Booth <duncan@NOSPAMrcp.co.uk> 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 duncan@rcp.co.uk
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 06-30-2003 03:03 PM

Re: Idiomatic way of repeating items in a sequence.

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 06-30-2003 03:35 PM

Re: Idiomatic way of repeating items in a sequence.

"alr" <anlri@wmdata.com> wrote in message
> 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 06-30-2003 03:58 PM

Re: Idiomatic way of repeating items in a sequence.

>>>>> "alr" == alr <anlri@wmdata.com> 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 06-30-2003 03:59 PM

Re: Idiomatic way of repeating items in a sequence.

>>>>> "alr" == alr <anlri@wmdata.com> 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

All times are GMT. The time now is 10:06 AM.