Python Iterable Types
Python Iterable Types
An iterable in Python can have zero, one, or several elements. An iterable can return its elements based on the user’s needs.
Due to this capability, we can use the Python for loop to iterate over an iterable.
In reality, the range() method is iterable because its output can be iterated over.
Code
# Python program to show that we can use a for loop to loop over an iterable object
# initializing a for loop
for item in range(5):
print(item)
Output
0
1
2
3
4
A string in Python is also an iterable object because we can use a for loop to iterate over its elements.
Code
# Python program to show that string is an iterable object by looping over it
# Initializing a string object
string = 'Python'
# Looping over the string object
for item in string:
print(item)
Output
P
y
t
h
o
n
Iterables include lists, primitives, and dictionaries because they can be looped over. As an example.
Code
# Python program to show that list, tuple, and dictionary are iterable objects by looping over them
#Initializing a list object
list_ = [1, 2, 3, 4, 5, 6]
print(f"Elements of the list {list_}: ")
# Looping over the list object
for item in list_:
print(item)
#Initializing a tuple object
tuple_ = (1, 2, 3, 4, 5, 6)
print(f"Elements of the tuple {tuple_}: ")
# Looping over the tuple object
for item in tuple_:
print(item)
#Initializing a dictionary object
dict_ = {'a': 1, 'b': 2, 'c': 3}
print(f"Elements of the dictionary {dict_}: ")
# Looping over the dictionary object
for key, value in dict_.items():
print(key, value)
Output
Elements of the list [1, 2, 3, 4, 5, 6]:
1
2
3
4
5
6
Elements of the tuple (1, 2, 3, 4, 5, 6):
1
2
3
4
5
6
Elements of the dictionary {'a': 1, 'b': 2, 'c': 3}:
a 1
b 2
c 3
As a general rule, if one can loop over anything, then it is iterable.
What is an Iterator?
Only iterables can be iterated. The component that performs iteration is called an iterator.
The iter() method returns an iterable object from a given iterable object. For example.
Code
# Python program to show how iter() is used
# Creating a list object
list_ = [1, 2, 3, 4, 5, 6]
# Creating the iter object
list_iter = iter(list_)
print(list_iter)
# Checking the type of list_iter object
print("The type of iter_list object is: ", type(list_iter))
Output
<list_iterator object at 0x7f56952b54f0>
The type of iter_list object is: <class 'list_iterator'>
Once we have an iterator object, we can use next() Method gets the next item from the iterator.
Code
# Python program showing how to get the elements of the iterator object
# Creating a list object
list_ = [1, 2, 3, 4, 5, 6]
# Creating the iter object
list_iter = iter(list_)
print(list_iter)
# Using the next() function to get the elements of the iterator list_iter
e = next(list_iter)
print(e)
Output
<list_iterator object at 0x7f56952b5b50>
1
Each use of the next() method returns the next item from the iterator. As an example.
Code
# Python program to use multiple next() statements
# Creating a list object
list_ = [1, 2, 3, 4, 5, 6]
# Creating the iter object
list_iter = iter(list_)
print(list_iter)
# Using the next() function five times to get the elements of the iterator list_iter
for i in range(5):
e = next(list_iter)
print(e)
Output
<list_iterator object at 0x7f56951d6c40>
1
2
3
4
5
If there are no more elements and we call the next() function, we will get an exception.
Code
# Python program to show that error is raised if we call the next() function more times than the number of elements
# Creating a list object
list_ = [1, 2, 3, 4, 5, 6]
# Creating the iter object
list_iter = iter(list_)
print(list_iter)
# Using the next() function seven times to get the elements of the iterator list_iter
for i in range(7):
e = next(list_iter)
print(e)
Output
<list_iterator object at 0x7f56952b7100>
1
2
3
4
5
6
----------------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-14-4456e4ae5daa> in <module>
11
12 for i in range(7):
---> 13 e = next(list_iter)
14 print(e)
15
StopIteration:
An iterator is a state operator. It means that after we take an item from a particular iterator, it is no longer usable.
In other words, after we loop over a given iterator, it becomes empty. If we iterate over it further, it will return nothing.
Because an iterator can be iterated, it is also considered an iterable object in Python. This is quite confusing. As an example.
Code
# Python program to show that an iterator is also an iterable object
# Creating a list and an iterator
list_ = [1, 2, 3, 4, 5, 6]
iter_ = iter(list_)
# Looping over an iterator
for l in iter_:
print(l)
Output
1
2
3
4
5
6
If we call the iter() method and provide it with an iterable, it will return the same iterator.
So, we’ve learned quite a bit about iterators in Python.