title: How to rewrite the code which appends to lists with numpy arrays
body: <p><a href="https://stackoverflow.com/questions/5064822/how-to-add-items-into-a-numpy-array">Here</a> is mentioned that appending to numpy array is not an good idea because it requires re-allocation of the array memory and it slows down the code. There are also mentioned some hints how to avoid it. My goal is to create an saw signal, something like this ASCII art: <code>/\/\/\/\</code>. I've end up with following two codes and I do not know which one is worse. 2nd one uses also <code>itertools.islice</code> which is also not world's speed recorder as it discussed <a href="https://stackoverflow.com/questions/18048698/efficient-iteration-over-slice-in-python">here</a>. Both codes mixes standard python lists and numpy arrays. Is there a way how can achieve the same but using pure numpy arrays? Or better: is there any general rule which I can use with numpy everywhere where I would use <code>list.append</code> instead? As I said on 1st page are mentioned some hints but I cannot figure out working solution. I've been thinking about something like list comprehension that will allow me expand the <code>saw</code> array first which then I could pass to <code>np.hstack</code> but I do not think this is possible. I've been also thinking about first declaring numpy array with <code>numpy.empty(400)</code> or <code>np.zeros(400)</code> but I do not know how to properly assign to them further values and seems that <code>np.empty</code> is not so completely empty because it contains zero values:</p>
  
  <pre><code>In [106]: x = np.zeros(10)
  In [107]: x
  Out[107]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
  
  In [108]: y = np.empty(10)
  In [109]: y
  Out[109]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
  </code></pre>
  
  <p>1st</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  saw = []
  for x in xrange(0, 3):
    saw.append(np.hstack((np.linspace(0,1,50), np.linspace(1,0,50))))
  saw2 = saw[0]
  for x in saw:
    saw2 = np.append([saw2], [x])
  plt.figure()
  plt.plot(np.arange(saw2.size), saw2)
  plt.show()
  </code></pre>
  
  <p>2nd</p>
  
  <pre><code>import numpy as np
  from itertools import islice
  import matplotlib.pyplot as plt
  
  saw = []
  for x in xrange(0, 4):
    saw.append(np.hstack((np.linspace(0,1,50), np.linspace(1,0,50))))
  saw2 = saw[0]
  for x in islice(saw, 1, None, 1):
    saw2 = np.append([saw2], [x])
  plt.figure()
  plt.plot(np.arange(saw2.size), saw2)
  plt.show()
  </code></pre>