title: How do I build a numpy array from a generator?
body: <p>How can I build a numpy array out of a generator object?</p>
  
  <p>Let me illustrate the problem:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy
  &gt;&gt;&gt; def gimme():
  ...   for x in xrange(10):
  ...     yield x
  ...
  &gt;&gt;&gt; gimme()
  &lt;generator object at 0x28a1758&gt;
  &gt;&gt;&gt; list(gimme())
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  &gt;&gt;&gt; numpy.array(xrange(10))
  array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  &gt;&gt;&gt; numpy.array(gimme())
  array(&lt;generator object at 0x28a1758&gt;, dtype=object)
  &gt;&gt;&gt; numpy.array(list(gimme()))
  array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  </code></pre>
  
  <p>In this instance, <code>gimme()</code> is the generator whose output I'd like to turn into an array. However, the array constructor does not iterate over the generator, it simply stores the generator itself. The behaviour I desire is that from <code>numpy.array(list(gimme()))</code>, but I don't want to pay the memory overhead of having the intermediate list and the final array in memory at the same time. Is there a more space-efficient way?</p>