title: Creating fixed length numpy array from variable length lists
body: <p>I have been generating variable length lists, a simplified example.</p>
  
  <pre><code>list_1 = [5] * 5
  list_2 = [8] * 10
  </code></pre>
  
  <p>I then want to convert to np.array for manipulation.  As such they need to be the same length (e.g. 1200), with the tail either populated with zeros, or truncated at the target length.</p>
  
  <p>For a fixed length of 8, I considered setting up a zero array then filling the appropriate entries:</p>
  
  <pre><code>np_list_1 = np.zeros(8)
  np_list_1[0:5] = list_1[0:5]
  np_list_2 = np.zeros(8)
  np_list_2[0:8] = list_2[0:8]     # longer lists are truncated
  </code></pre>
  
  <p>I've created the following function to produce these</p>
  
  <pre><code>def get_np_fixed_length(list_like, length):
      list_length = len(list_like)
      np_array = np.zeros(length)
      if list_length &lt;= length:
          np_array[0:list_length] = list_like[:]
      else:
          np_array[:] = list_like[0:length]
      return np_array
  </code></pre>
  
  <p>Is there a more efficient way to do this? (I couldn't see anything in numpy documentation)</p>