how to append to a list twice?
John Salerno
johnjsal at NOSPAMgmail.com
Fri Apr 21 11:46:07 EDT 2006
Paul McGuire wrote:
> "John Salerno" <johnjsal at NOSPAMgmail.com> wrote in message
> news:ui62g.1991$No6.43121 at news.tufts.edu...
>> 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 x in range(10): # just for testing
> series.extend([series[-1] - 1]*2)
>
>
Interesting. I tried the *2 method twice, but I kept getting weird
results, I guess because I was using append and not extend. I thought
extend added lists to lists, but obviously that's not the case here.
Thanks.
More information about the Python-list
mailing list