Python Iterators
Python Iterators
Menu Log in
Dark mode
Dark code
HTML CSS
Python Iterators
❮ Previous Next ❯
Python Iterators
An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that you can traverse
through all the values.
Iterator vs Iterable
Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable
containers
which you can get an iterator from.
All these objects have a iter() method which is used to get an iterator:
Example
Return an iterator from a tuple, and print each value:
https://www.w3schools.com/python/python_iterators.asp 1/9
5/18/22, 2:28 PM Python Iterators
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
Try it Yourself »
Example
Strings are also iterable objects, containing a sequence of characters:
mystr = "banana"
myit = iter(mystr)
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
Try it Yourself »
Example
Iterate the values of a tuple:
https://www.w3schools.com/python/python_iterators.asp 2/9
5/18/22, 2:28 PM Python Iterators
for x in mytuple:
print(x)
Try it Yourself »
Example
Iterate the characters of a string:
mystr = "banana"
for x in mystr:
print(x)
Try it Yourself »
The for loop actually creates an iterator object and executes the next() method for
each loop.
Create an Iterator
To create an object/class as an iterator you have to implement the methods
__iter__()
and __next__() to your object.
As you have learned in the Python Classes/Objects chapter, all classes have a function
called
__init__() , which allows you to do some initializing when the object is being
created.
The __iter__() method acts similar, you can do operations (initializing etc.), but must
always return the iterator object itself.
https://www.w3schools.com/python/python_iterators.asp 3/9
5/18/22, 2:28 PM Python Iterators
The __next__() method also allows you to do operations, and must return the next
item in the sequence.
Example
Create an iterator that returns numbers, starting with 1, and each sequence will increase
by one (returning 1,2,3,4,5 etc.):
class MyNumbers:
def __iter__(self):
self.a = 1
def __next__(self):
x = self.a
self.a += 1
return x
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
Try it Yourself »
StopIteration
The example above would continue forever if you had enough next() statements, or if it
was used in a for loop.
In the __next__() method, we can add a terminating condition to raise an error if the
iteration is done a specified number of times:
https://www.w3schools.com/python/python_iterators.asp 4/9
5/18/22, 2:28 PM Python Iterators
Example
Stop after 20 iterations:
class MyNumbers:
def __iter__(self):
self.a = 1
def __next__(self):
x = self.a
self.a += 1
return x
else:
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)
Try it Yourself »
❮ Previous Next ❯
NEW
https://www.w3schools.com/python/python_iterators.asp 5/9
5/18/22, 2:28 PM Python Iterators
We just launched
W3Schools videos
Explore now
COLOR PICKER
Get certified
by completing
a course today!
school
w3 s
2
CE
02
TI 2
R
FI .
ED
Get started
CODE GAME
https://www.w3schools.com/python/python_iterators.asp 6/9
5/18/22, 2:28 PM Python Iterators
Play Game
Report Error
Forum
About
Shop
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
https://www.w3schools.com/python/python_iterators.asp 7/9
5/18/22, 2:28 PM Python Iterators
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Web Courses
HTML Course
CSS Course
JavaScript Course
SQL Course
Python Course
PHP Course
jQuery Course
Java Course
C++ Course
C# Course
XML Course
Get Certified »
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant
full correctness of all content.
While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.
https://www.w3schools.com/python/python_iterators.asp 8/9
5/18/22, 2:28 PM Python Iterators
https://www.w3schools.com/python/python_iterators.asp 9/9