In Python, the for
loop is used to iterate over the elements of different data types. For example, we can iterate over strings, lists, tuples, dictionaries, and files. Have you ever wondered how the for
loop determines how to pick letters from strings, elements of a list, tuples, and key-value pairs from a dictionary?
In this shot, we'll discuss how the for
loop works.
for
loopPython implements iterator-based loops. It doesn’t know the underlying object type. No matter what kind of object we pass, the for
loop is not going to determine its type to iterate over it. The only thing it checks is if the object passed to it is iterable or not. If the object is iterable, it asks for the next element, until it reaches the end of the object.
The diagram below summarises the working of a for
loop:
for
loop, it checks if that object is iterable. If it is not, it throws an error. We iterate over an integer object in this example:
num = 10for i in num:print(i)
Internally, for
calls the iter()
function on num
, as shown below:
iter(num)
Since integer objects are not iterable, an error, TypeError
, is raised:
TypeError: ‘int’ object is not iterable
We run the code in the IDE below and see the output:
num = 10for i in num:print(i)
We iterate over a list object in the below example:
num_list= [1,2,3,4,5]for i in num_list:print(i)
Internally, for
calls the iter()
function on num_list
. It returns a list_iterator
object:
iter(num_list)<list_iterator object at 0x0000021CBBC58E20>
The for
loop calls the next()
function to determine the next element from the iterator object until it throws the StopIteration
exception:
>>num_list_iterator=iter(num_list)>>print(next(num_list_iterator))1>>print(next(num_list_iterator))2>>print(next(num_list_iterator))3>>print(next(num_list_iterator))4>>print(next(num_list_iterator))5>>print(next(num_list_iterator))Traceback (most recent call last):File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcodeexec(code, self.locals)File "<input>", line 1, in <module>StopIteration
We can run the code in the IDE below and see the output:
num_list= [1,2,3,4,5]for i in num_list:print(i)
We will iterate over a string object in this example:
my_str="Hello"for i in my_str:print(i)
Internally, for
calls the iter()
function on my_str
and it returns a str_iterator
object:
iter(my_str)<str_iterator object at 0x0000021CBBC59EA0>
The for
loop calls the next()
function to determine the next element from the iterator object until throws the StopIteration
exception:
>>my_str_iterator=iter(my_str)>>print(next(my_str_iterator))H>>print(next(my_str_iterator))e>>print(next(my_str_iterator))l>>print(next(my_str_iterator))l>>print(next(my_str_iterator))oprint(next(my_str_iterator))Traceback (most recent call last):File "C:\Users\paian\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcodeexec(code, self.locals)File "<input>", line 1, in <module>StopIteration
We can run the code in the IDE below and see the output:
my_str="Hello"for i in my_str:print(i)
Free Resources