Skip to content

Instantly share code, notes, and snippets.

@graingerkid
Created June 18, 2015 10:55
Show Gist options
  • Save graingerkid/6ce29be1f4bc521525af to your computer and use it in GitHub Desktop.
Save graingerkid/6ce29be1f4bc521525af to your computer and use it in GitHub Desktop.
Helper function to split a generator into a specified size.
def last_50(generator, n):
'''
Takes a generator object and returns
a specified number (n) into a list.
'''
counter = 0
results = []
for i in generator:
if counter == n:
break
else:
results.append((i))
counter += 1
return results
def gen():
'''
This is an example generator
for testing the last_50() function
'''
for i in range(10000000):
yield i
print last_50(gen(), 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment