Iterator in Python is any python type that can be used with a ' for in loop '. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite. Such type of iterators are known as Infinite iterators.
In Python, the functions itertools. count() , itertools. cycle() , and itertools. repeat() in the standard library itertools module can be used to create infinite iterators.
Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions ( yield statements, in Python 2.5 and earlier), and is an object that meets the previous paragraph's definition of an iterator .
Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration. next ( __next__ in Python 3) The next method returns the next value for the iterable.
itertools
provides three infinite iterators:
count(start=0, step=1)
: 0, 1, 2, 3, 4, ...
cycle(p)
: p[0], p[1], ..., p[-1], p[0], ...
repeat(x, times=∞)
: x, x, x, x, ...
I don't know of any others in the standard library.
Since you asked for a one-liner:
__import__("itertools").count()
for x in iter(int, 1): pass
iter
= zero-argument callable + sentinel valueint()
always returns 0
Therefore, iter(int, 1)
is an infinite iterator. There are obviously a huge number of variations on this particular theme (especially once you add lambda
into the mix). One variant of particular note is iter(f, object())
, as using a freshly created object as the sentinel value almost guarantees an infinite iterator regardless of the callable used as the first argument.
you can iterate over a callable returning a constant always different than iter()'s sentinel
g1=iter(lambda:0, 1)
Your OS may provide something that can be used as an infinite generator. Eg on linux
for i in (0 for x in open('/dev/urandom')):
print i
obviously this is not as efficient as
for i in __import__('itertools').repeat(0)
print i
Quite ugly and crazy (very funny however), but you can build your own iterator from an expression by using some tricks (without "polluting" your namespace as required):
{ print("Hello world") for _ in
(lambda o: setattr(o, '__iter__', lambda x:x)
or setattr(o, '__next__', lambda x:True)
or o)
(type("EvilIterator", (object,), {}))() }
None that doesn't internally use another infinite iterator defined as a class/function/generator (not -expression, a function with yield
). A generator expression always draws from anoter iterable and does nothing but filtering and mapping its items. You can't go from finite items to infinite ones with only map
and filter
, you need while
(or a for
that doesn't terminate, which is exactly what we can't have using only for
and finite iterators).
Trivia: PEP 3142 is superficially similar, but upon closer inspection it seems that it still requires the for
clause (so no (0 while True)
for you), i.e. only provides a shortcut for itertools.takewhile
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With