title: Length of a finite generator
body: <p>I have these two implementations to compute the length of a finite generator, while keeping the data for further processing:</p>
  
  <pre><code>def count_generator1(generator):
      '''- build a list with the generator data
         - get the length of the data
         - return both the length and the original data (in a list)
         WARNING: the memory use is unbounded, and infinite generators will block this'''
      l = list(generator)
      return len(l), l
  
  def count_generator2(generator):
      '''- get two generators from the original generator
         - get the length of the data from one of them
         - return both the length and the original data, as returned by tee
         WARNING: tee can use up an unbounded amount of memory, and infinite generators will block this'''
      for_length, saved  = itertools.tee(generator, 2)
      return sum(1 for _ in for_length), saved
  </code></pre>
  
  <p>Both have drawbacks, both do the job. Could somebody comment on them, or even offer a better alternative?</p>