how to append to a list twice?

James Stroud jstroud at ucla.edu
Fri Apr 21 14:37:30 EDT 2006


John Salerno wrote:
> If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] 
> (where each item is repeated twice after the first one), how might I do 
> that most efficiently?
> 
> Right now I have this:
> 
> series = [100]
> for x in range(10):   # just for testing
>     series.append(series[-1] - 1)
> 
> But of course that only does it once, and I don't want to have to copy 
> and paste the append line. Perhaps there's a better way than this.
> 
> Thanks.

I know this doesn't beat Edward Elliot's answer, but:

py> import operator
py> [100] + reduce(operator.add, [[i,i] for i in xrange(99,90,-1)])
[100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, 
91, 91]


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list