Get a fixed number of elements from a generator

What is the most efficient way to get a fixed number of elements from a generator?

I am currently using zipand range. In this example, I take pieces of size 3 from the generator.

def f():
  x = 0
  while x < 21:
    yield x
    x += 1

g = f()

while True:
  x = [i for _, i in zip(range(3), g)]
  if not x:
    break
  print x

It is assumed that the database used provides a generator object for the query results. Than I fill an array with a fixed numpy size and process it as one batch.

+5
source share
1 answer

Use itertools.islice:

import itertools

for elem in itertools.islice(f(), 3):
    print elem

and directly into your numpy array:

my_arr = np.array(itertools.islice(f(), 3))
+6
source

All Articles