The following module functions all construct and return iterators. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream.
*iterables) |
def chain(*iterables): for it in iterables: for element in it: yield element
[n]) |
def count(n=0): while True: yield n n += 1
Note, count() does not check for overflow and will return
negative numbers after exceeding sys.maxint
. This behavior
may change in the future.
iterable) |
def cycle(iterable): saved = [] for element in iterable: yield element saved.append(element) while saved: for element in saved: yield element
Note, this is the only member of the toolkit that may require significant auxiliary storage (depending on the length of the iterable).
predicate, iterable) |
def dropwhile(predicate, iterable): iterable = iter(iterable) for x in iterable: if not predicate(x): yield x break for x in iterable: yield x
predicate, iterable) |
True
.
If predicate is None
, return the items that are true.
Equivalent to:
def ifilter(predicate, iterable): if predicate is None: predicate = bool for x in iterable: if predicate(x): yield x
predicate, iterable) |
False
.
If predicate is None
, return the items that are false.
Equivalent to:
def ifilterfalse(predicate, iterable): if predicate is None: predicate = bool for x in iterable: if not predicate(x): yield x
function, *iterables) |
None
, then
imap() returns the arguments as a tuple. Like
map() but stops when the shortest iterable is exhausted
instead of filling in None
for shorter iterables. The reason
for the difference is that infinite iterator arguments are typically
an error for map() (because the output is fully evaluated)
but represent a common and useful way of supplying arguments to
imap().
Equivalent to:
def imap(function, *iterables): iterables = map(iter, iterables) while True: args = [i.next() for i in iterables] if function is None: yield tuple(args) else: yield function(*args)
iterable, [start,] stop [, step]) |
None
, then iteration continues until
the iterator is exhausted, if at all; otherwise, it stops at the specified
position. Unlike regular slicing,
islice() does not support negative values for start,
stop, or step. Can be used to extract related fields
from data where the internal structure has been flattened (for
example, a multi-line report may list a name field on every
third line). Equivalent to:
def islice(iterable, *args): s = slice(*args) next, stop, step = s.start or 0, s.stop, s.step or 1 for cnt, element in enumerate(iterable): if cnt < next: continue if stop is not None and cnt >= stop: break yield element next += step
*iterables) |
def izip(*iterables): iterables = map(iter, iterables) while iterables: result = [i.next() for i in iterables] yield tuple(result)
Changed in version 2.3.1: When no iterables are specified, returns a zero length iterator instead of raising a TypeError exception.
object[, times]) |
def repeat(object, times=None): if times is None: while True: yield object else: for i in xrange(times): yield object
function, iterable) |
function(a,b)
and function(*c)
.
Equivalent to:
def starmap(function, iterable): iterable = iter(iterable) while True: yield function(*iterable.next())
predicate, iterable) |
def takewhile(predicate, iterable): for x in iterable: if predicate(x): yield x else: break
See About this document... for information on suggesting changes.