title: Fill a bytearray of known size from generator
body: <p>Outgoing from a formerly given or computed very long array (MB, GB, TB) of small bytesize numbers (so I use a <code>bytearray</code>), I need to compute in an next iteration step a follow-up <code>bytearray</code>. It is possible to compute the size which is needed for next iteration step <code>bytearray</code>, so I can allocate the memory beforehand by using one of the constructors for <code>bytearray</code>:</p>
  <pre class="lang-py prettyprint-override"><code># A is the current/former bytearray
  # sizes of array: 1 -&gt; 2 -&gt; 8 -&gt; 48 -&gt; 480 -&gt; 5_760 -&gt; 92_160 -&gt; 1_658_880 -&gt; 
  # 36_495_360 -&gt; 1_021_870_080 -&gt; 30_656_102_400 [ -&gt; 1_103_619_686_400 ... ]
  ls = NextLenArray(A)
  L = bytearray(ls)
  
  # generator will create new values out of the current existing
  for i,j in enumerate(gen_values(A)):
      L[i] = j
  
  # need to assign it back into A for next iteration
  A = L
  </code></pre>
  <p>Alternatively, it's obvious to create directly the next <code>bytearray</code> by using generator inside comprehension. I don't know how and when (stepwise?) reservation of memory will happen.</p>
  <pre class="lang-py prettyprint-override"><code>A = bytearray(j for j in gen_values(A))
  </code></pre>
  <p>It looks like that it runs a little faster, but monitoring task manager it uses more memory while generating and in later iteration steps it get stopped one step earlier caused be <code>MemoryError</code>.</p>
  <p>Is there an easy way to combine the pre-reservation by assigning a <code>bytearray</code> with needed size and use this with generator/comprehension-list?</p>