how to append to a list twice?

Gerard Flanagan grflanagan at yahoo.co.uk
Fri Apr 21 11:13:50 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.

series = [100]

for i in range(1,10):
    series.extend([100-i]*2)

print series

[100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92,
91, 91]

Gerard




More information about the Python-list mailing list