Iterator: Python String Python List Python Dictionary
Iterator: Python String Python List Python Dictionary
Iterator: Python String Python List Python Dictionary
Python has several built-in objects, which implement the iterator protocol. For
example lists, tuples, strings, dictionaries.
You may argue that the “first” number in number is 1, but it is not the same as
the first item in a sequence.
It doesn’t make sense to ask “What is the number after 1?” from number since
Python only understands integers as a single entity.
For example, lists tell the for loop that the next item to iterate on it is the
index+1 from the current one. Also an iterable must also signal to a for loop
when to stop iterating. This signal usually comes when we arrive at the end of
a sequence (i.e. the end of a list or string).
An iterator protocol specifies the following two steps:
Cleaner code
Iterators can work with infinite sequences
Iterators help to produce cleaner looking code because they allows us to work
with infinite sequences without having to reallocate resources for every possible
sequence, thus also saving resource space. Python has several built-in objects,
which implement the iterator protocol and you must have seen some of these
before: lists, tuples, strings, dictionaries and even files
An iterable is any object, not necessarily a data structure that can return an
iterator". Its main purpose is to return all of its elements. Iterables can
represent finite as well as infinite source of data. An iterable will directly or
indirectly define two methods: the __iter__() method, which must return the
iterator object and the __next()__ method with the help of the iterator it calls.
myset = {1, 2, 3}
myiterator = iter(myset)
next(myiterator)
print(type(myset))
print(type(myiterator))
----------------------------------------------------------------------
colourlist =
["Purple","Blue","Pink","Red","Orange","Yellow","Green","White","Black
"]
itercol = iter(colourlist)
#print(itercol)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
colour = next(itercol)
print(colour)
---------------------------------------------------------------------
Let us create a class by name EmailContacts containing the email contacts of
all your customers.
To add iteration support to this class you have to follow the iterator protocol.
To do this, you have to both make the EmailContacts an iterable and make it
return an iterator object. The __iter__ method’s implementation achieves both
of these things.
class EmailContacts:
def __init__(self, emails):
self.emails = emails
self.index = 0
def __iter__(self):
return self
def __next__(self):
try:
email = self.emails[self.index]
except IndexError:
raise StopIteration()
self.index += 1
return email
In the following code we get the iterator object and iterate over it without a
looping construct. The iter(type) method calls the underlying __iter__ method.
In our case the __iter__ is implemented, which makes the EmailContacts an
iterable and also gets us an iterator object.
print(iterator.__next__())
print(iterator.__next__())
print(iterator.__next__())
print(iterator.__next__())
So essentially these are all things that happen behind the scenes when you
iterate over an iterable using a for loop (or in any other iteration context).
----------------------------------------------------------------------
class fibo:
def __init__(self):
# default constructor
self.prev = 0
self.cur = 1
self.n = 1