[ Web Proxy ]
URL:
Viewing: https://python-course.eu/advanced-python/iterable-iterator.php [Back]  [Original]

2. Iterators and Iterables | Advanced | python-course.eu
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Advanced Python
  2. Recursive Functions
  3. Iterators and Iterables
  4. Generators and Iterators
  5. Lambda Operator, filter, reduce and map
  6. zip introduction and examples
  7. Decorators and Decoration
  8. Memoization and Decorators
  9. Functional Programming OOP
  10. List Comprehension
  11. Function Composition In Python
  12. Currying in Python
  13. Argument Count
  14. Tests, DocTests, UnitTests
  15. Testing with Pytest
  16. Regular Expressions
  17. Advanced Regular Expressions

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

2. Iterators and Iterables

By Bernd Klein. Last modified: 01 Feb 2022.

The Python forums and other question-and-answer websites like Quora and Stackoverflow are full of questions concerning 'iterators' and 'iterable'. Some want to know how they are defined and others want to know if there is an easy way to check, if an object is an iterator or an iterable. We will provide a function for this purpose.

We have seen that we can loop or iterate over various Python objects like lists, tuples and strings. For example:

for city in ["Berlin", "Vienna", "Zurich"]:
    print(city)
for language in ("Python", "Perl", "Ruby"):
    print(city)
for char in "Iteration is easy":
    print(char)

OUTPUT:

Berlin
Vienna
Zurich
Python
Perl
Ruby
I
t
e
r
a
t
i
o
n
i
s
e
a
s
y

This form of looping can be seen as iteration. Iteration is not restricted to explicit for loops. If you call the function sum, - e.g. on a list of integer values, - you do iteration as well.

Difference between Iterators and Iterables [Difference between Iterators and Iterables]

So what is the difference between an iterable and an iterator?

On one hand, they are the same: You can iterate with a for loop over iterators and iterables. Every iterator is also an iterable, but not every iterable is an iterator. E.g. a list is iterable but a list is not an iterator! An iterator can be created from an iterable by using the function 'iter'. To make this possible the class of an object needs either a method '__iter__', which returns an iterator, or a '__getitem__' method with sequential indexes starting with 0.

Iterators are objects with a '__next__' method, which will be used when the function 'next()' is called.

So what is going on behind the scenes, when a for loop is executed? The for statement calls iter() on the object ( which should be a so-called container object), over which it is supposed to loop . If this call is successful, the iter call will return an iterator object that defines the method __next__() which accesses elements of the object one at a time. The __next__() method will raise a StopIteration exception, if there are no further elements available. The for loop will terminate as soon as it catches a StopIteration exception. You can call the __next__() method using the next() built-in function. This is how it works:

cities = ["Berlin", "Vienna", "Zurich"]
iterator_obj = iter(cities)
print(iterator_obj)
print(next(iterator_obj))
print(next(iterator_obj))
print(next(iterator_obj))

OUTPUT:

<list_iterator object at 0x0000016FBDAEEC88>
Berlin
Vienna
Zurich

If we called 'next(iterator_obj)' one more time, it would return 'StopIteration'

The following function 'iterable' will return True, if the object 'obj' is an iterable and False otherwise.

def iterable(obj):
     try:
         iter(obj)
         return True
     except TypeError:
         return False 
for element in [34, [4, 5], (4, 5), {"a":4}, "dfsdf", 4.5]:
    print(element, "iterable: ", iterable(element))

OUTPUT:

34 iterable:  False
[4, 5] iterable:  True
(4, 5) iterable:  True
{'a': 4} iterable:  True
dfsdf iterable:  True
4.5 iterable:  False

We have described how an iterator works. So if you want to add an iterator behavior to your class, you have to add the __iter__ and the __next__ method to your class. The __iter__ method returns an iterator object. If the class contains a __next__, it is enough for the __iter__ method to return self, i.e. a reference to itself:

class Reverse:
    """
    Creates Iterators for looping over a sequence backwards.
    """
    def __init__(self, data):
        self.data = data
        self.index = len(data)
    def __iter__(self):
        return self
    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]
lst = [34, 978, 42]
lst_backwards = Reverse(lst)
for el in lst_backwards:
    print(el)

OUTPUT:

42
978
34

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page