title: Pre-allocating a list of None
body: <p>Suppose you want to write a function which yields a list of objects, and you know in advance the length <code>n</code> of such list.</p>
  
  <p>In python the list supports indexed access in O(1), so it is arguably a good idea to pre-allocate the list and access it with indexes instead of allocating an empty list and using the <code>append()</code> method. This is because we avoid the burden of expanding the whole list if the space is not enough.</p>
  
  <p>If I'm using python, probably performances are not that relevant in any case, but what is the better way of pre-allocating a list?</p>
  
  <p>I know these possible candidates:</p>
  
  <ul>
  <li><code>[None] * n</code> → allocating two lists</li>
  <li><code>[None for x in range(n)]</code> — or <code>xrange</code> in python2 → building another object</li>
  </ul>
  
  <p>Is one significantly better than the other?</p>
  
  <p>What if we are in the case <code>n = len(input)</code>? Since <code>input</code> exists already, would <code>[None for x in input]</code> have better performances w.r.t. <code>[None] * len(input)</code>?</p>