Is there a better way of doing this?

Peter Otten __peter__ at web.de
Sat Mar 7 13:28:27 CET 2009


Lie Ryan wrote:

> mattia wrote:

>> Yes, sorry, I have to recycle! But how about this:

>>>>> rw = [[2,4], [4,5,6],[5,5]]
>>>>> rw += [[1,1]]*2
>>>>> rw
>> [[2, 4], [4, 5, 6], [5, 5], [1, 1], [1, 1]]

>> How can I recicle in this way using append?
> 
> Not .append() but .extend()

Whether you use

items += [item]*N

or

items.extend([item]*N)

is mostly a matter of style. You can avoid the intermediate list with

items.extend(itertools.repeat(item, N))

but I don't think this approach is faster.

Peter



More information about the Python-list mailing list