Python Iterators
Python Tutorial » Python Inheritance
An iterator is an object that contains a countable number of values and
is used to iterate over iterable objects like lists, tuples, dicts, and sets.
Python iterator object must implement two special methods," __iter__()" and "__next__()" (without quotes),
called the iterator protocol.
Create python inbuilt iterator
How iterator is working in python.
Example:
Output:
p
y
t
o
n
t
u
t
o
r
i
a
l
my_iterable_value = 'pyton tutorial'
my_iterable_obj = iter(my_iterable_value)
while True:
try:
# Iterate by calling next
item = next(my_iterable_obj)
print(item)
except StopIteration:
break
my_iterable_obj = iter(my_iterable_value)
while True:
try:
# Iterate by calling next
item = next(my_iterable_obj)
print(item)
except StopIteration:
break
Output:
p
y
t
o
n
t
u
t
o
r
i
a
l
Iterator that creates iterator type
Simple Python program to demonstrate # working of iterators
Example:
104
105
106
107
108
109
110
111
112
class Test:
# Constructor
def __init__(self, limit):
self.limit = limit
# Creates iterator object
# Called when iteration is initialized
def __iter__(self):
self.x = 104
return self
# To move to next element. In Python 3,
# we should replace next with __next__
def __next__(self):
# Store current value ofx
x = self.x
# Stop iteration if limit is reached
if x > self.limit:
raise StopIteration
# Else increment and return old value
self.x = x + 1;
return x
# Print numbers from 104 to 112
for i in Test(112):
print(i)
# Print nothing
for i in Test(103):
print(i)
Output:# Constructor
def __init__(self, limit):
self.limit = limit
# Creates iterator object
# Called when iteration is initialized
def __iter__(self):
self.x = 104
return self
# To move to next element. In Python 3,
# we should replace next with __next__
def __next__(self):
# Store current value ofx
x = self.x
# Stop iteration if limit is reached
if x > self.limit:
raise StopIteration
# Else increment and return old value
self.x = x + 1;
return x
# Print numbers from 104 to 112
for i in Test(112):
print(i)
# Print nothing
for i in Test(103):
print(i)
104
105
106
107
108
109
110
111
112
Iterating over a list, tuple, string, dictionary
Sample built-in iterators
Example:
Python List Iteration
agernic
python
tutorial
Python Tuple Iteration
agernic
python
tutorial
Python String Iteration
T
u
t
o
r
i
a
l
Python Dictionary Iteration
xyz 468
abc 215
# Python iterating over list
print("Python List Iteration")
l = ["agernic", "python", "tutorial"]
for i in l:
print(i)
# Python iterating over tuple (immutable)
print("\nPython Tuple Iteration")
t = ("agernic", "python", "tutorial")
for i in t:
print(i)
# Python iterating over String
print("\nPython String Iteration")
s = "Tutorial"
for i in s :
print(i)
# Python iterating over dictionary
print("\nPython Dictionary Iteration")
d = dict()
d['xyz'] = 468
d['abc'] = 215
for i in d :
print("%s %d" %(i, d[i]))
Output:print("Python List Iteration")
l = ["agernic", "python", "tutorial"]
for i in l:
print(i)
# Python iterating over tuple (immutable)
print("\nPython Tuple Iteration")
t = ("agernic", "python", "tutorial")
for i in t:
print(i)
# Python iterating over String
print("\nPython String Iteration")
s = "Tutorial"
for i in s :
print(i)
# Python iterating over dictionary
print("\nPython Dictionary Iteration")
d = dict()
d['xyz'] = 468
d['abc'] = 215
for i in d :
print("%s %d" %(i, d[i]))
Python List Iteration
agernic
python
tutorial
Python Tuple Iteration
agernic
python
tutorial
Python String Iteration
T
u
t
o
r
i
a
l
Python Dictionary Iteration
xyz 468
abc 215
Python Iterators, and generators, vs generators, vs iterables, explained, python iterator next, python iterator vs generator, python iterator to list
Python Iterators - python
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.