title: Padding or truncating a Python list
body: <p>I'd like to truncate or pad a list. E.g. for size 4:</p>
  
  <pre><code>[1,2,3] -&gt; [1,2,3,0]
  [1,2,3,4,5] -&gt; [1,2,3,4]
  </code></pre>
  
  <p>I can see a couple of ways:</p>
  
  <pre><code>def trp(l, n):
      """ Truncate or pad a list """
      r = l[:n]
      if len(r) &lt; n:
          r.extend([0] * (n - len(r)))
      return r
  </code></pre>
  
  <p>Or a shorter, but less efficient:</p>
  
  <pre><code>map(lambda x, y: x if x else 0, m[0:n], [0] * n)
  </code></pre>
  
  <p>Is there a more elegant way of doing this?</p>