how to append to a list twice?

Sion Arrowsmith siona at chiark.greenend.org.uk
Fri Apr 21 12:31:25 EDT 2006


John Salerno  <johnjsal at NOSPAMgmail.com> 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, [ ... ]

You could try:

series = [100]
l = range(series[-1]-1, series[-1]-11, -1) #just for testing
s.extend(sum(zip(l, l), ()))

Times as faster than the series.extend([series[-1] - 1] * 2)
for me.

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list