June 2017
Beginner
352 pages
8h 39m
English
The first generator we'll look at is take() which retrieves a specified number of elements from the front of a sequence:
def take(count, iterable): """Take items from the front of an iterable. Args: count: The maximum number of items to retrieve. iterable: The source of the items. Yields: At most 'count' items from 'iterable'. """ counter = 0 for item in iterable: if counter == count: return counter += 1 yield item
Read now
Unlock full access