An iteratable is a Python object that can be used as a sequence. You can go to the next item of the sequence using the next() method.
You can loop over an iterable, but you cannot access individual elements directly.
It’s a container object: it can only return one of its element at the time.
Related course: Complete Python Bootcamp: Go from zero to hero in Python 3
Define a dictionary and an iterable like the code below:
You can loop over the items like so:
for item in iterable:
Create an iterator to use the next method:
You cannot access elements using an index, this will raise an exception.
Iterable object types includes lists, strings, dictionaries and sets.
The demo below applies an iterator to a list:
items = [ "one","two","three","four" ]
Try the exercises below
- What is an iterable?
- Which types of data can be used with an iterable?
You can download the answers below: