Understanding for-loop in Python
Last Updated :
29 Dec, 2019
A Pythonic for-loop is very different from for-loops of other programming language. A for-loop in Python is used to loop over an iterator however in other languages, it is used to loop over a condition. In this article, we will take a deeper dive into Pythonic for-loop and witness the reason behind this dissimilarity. Let's begin by familiarizing ourselves with the looping gotchas:
Gotchas in for-loop
If one doesn't know what "gotcha" means:
a "gotcha" in coding is a term used for a feature of a programming language (say for-loop, function, return statement, etc) that is likely to play tricks by showing a behavior which doesn't match the expected outcome. Here are two infamous for-loop gotchas:
Consider this example:
Python3 1==
numList = [0, 1, 2, 3, 4]
squares = (n**2 for n in numList)
Here, the variable
squares
contains an iterable of squares of the elements of
numList
. If we check whether 16 is in
squares
, we get True but if we check it again, we get False.
sum of a for-loop :
Take a look at this:
Python3 1==
numList = [0, 2, 4, 6, 8]
doubles = (n * 2 for n in numList)
We can make this
doubles
into a list or tuple to look at its elements. Let's calculate the sum of the elements in doubles. The result should be 40 as per expectation.
But, we get 0 instead.

To understand this anomaly, let's first see "under-the-hood" working of for-loops.
Inside a for-loop
As stated earlier, for-loops of other programming languages, such as C, C++, Java, loop over a condition. For example:
javascript
let numList = [0, 1, 2, 3, 4];
for (let i = 0; i < numList.length; i += 1) {
print(numList[i])
}
The above code is written in Javascript. As seen the for-loop is quite different from what we see in Python. This is because what we call a for-loop in Python is actually a "
foreach" loop. A foreach loop doesn't maintain a counter like a for loop. A for loop works on indices of the elements of the iterable rather than the element itself. But a foreach loop works straight on the elements rather than their indices and thus have no conditions, no initializations and no incrementation of index.
Python3 1==
numList = [0, 1, 2, 3, 4]
# for loop in Python
for n in numList:
print(n)
Hence, it won't be wrong to say that we don't have for-loops in Python but we have foreach loops which are implemented as for-loops!
One might think that Python uses indices under the hood to loop over in a for loop. But the answer is no. Let's look at an example to prove this:
We will take the help of while loop for using indices.
CPP
games = { 'tennis', 'baseball', 'rugby', 'soccer' }
i = 0
while i < len(games):
print(games[i])
i += 1
Output:

This proves that Python doesn't make use of indices for looping and so we can't loop over everything using indices. A simple question now arises, what does Python use for looping? The answer is, iterators!
Iterators
We know what iterables are(lists, strings, tuples, etc). An iterator can be considered as the power supply of iterables. An iterable is made up of iterator and this is the fact which helps Python to loop over an iterable. To extractor iterator from an iterable, we use Python's
iter
function.

Let's look at an example:
Python3 1==
games = ['tennis', 'baseball', 'rugby', 'soccer']
iterator = iter(games)
# we use the next() function to
# print the next item of iterable
print(next(iterator))
print(next(iterator))
print(next(iterator))
If we keep on using the
next()
function even after we reach the last item, we will get a StopIteration Error.
Note: Once an item in an iterator is all used up(iterated over), it is deleted from the memory!

Now that we know how loops work, let's try and make our own loop using the power of iterators.
Python3 1==
# Python program to demonstrate
# power of iterators
# creating our own loop
def newForLoop(iterable):
# extracting iterator out of iterable
iterator = iter(iterable)
# condition to check if looping is done
loopingFinished = False
while not loopingFinished:
try:
nextItem = next(iterator)
except StopIteration:
loopingFinished = True
else:
print(nextItem)
# Driver's code
newForLoop([1, 2, 3, 4])
Output:
1
2
3
4
We need to learn about iterators because we work with iterators almost every time with even knowing about it. The most common example is a generator. A generator is an iterator. We can apply each and every function of an iterator to a generator.
Python3 1==
numList = [0, 2, 4]
# creating a generator named "squares"
squares = (n**2 for n in numList)
print(next(squares))
print(next(squares))
print(next(squares))
Output:
0
4
16
Resolving looping gotchas
Now that we know what exactly are for-loops and how do they work in Python, we will end this article from where we began, that is, by trying to reason out looping gotchas as seen earlier.
Exhausting an iterator partially :
When we did :
Python3 1==
numList = [0, 1, 2, 3, 4]
squares = (n**2 for n in numList)
and asked if 9 is in the
squares
, we got True. But asking again gives returns a False. This is because when we first asked if 9 is there, it iterates over the iterator(generator) to find 9 and we know that as soon as the next item in an iterator is reached, the previous item is deleted. This is why once we find 9, all the numbers before 9 get deleted and asking again returns False. Hence we have exhausted the iterator partially.
Exhausting an iterator completely :
In this code:
Python3 1==
numList = [0, 2, 4, 6, 8]
doubles = (n * 2 for n in numList)
When we convert
doubles
to list, we are already iterating over each item in the iterator by doing so. Therefore, the iterator gets completely exhausted and finally, no items remain in it. This is why the function
sum()
on the tuple returns zero. If we do the sum without converting it into a list, it will return the correct output.
Similar Reads
Unused variable in for loop in Python Prerequisite: Python For loops The for loop has a loop variable that controls the iteration. Not all the loops utilize the loop variable inside the process carried out in the loop. Example: Python3 # i,j - loop variable # loop-1 print("Using the loop variable inside :") # used loop variabl
3 min read
Underscore (_) in Python In Python, underscores have different meanings depending on how they are used. The single underscore (_) and double underscore (__) each serve specific purposes. They are commonly used in different situations, such as for variable names, method names, and more. Single UnderscoreSingle underscore (_)
5 min read
Looping Techniques in Python Python supports various looping techniques by certain inbuilt functions, in various sequential containers. These methods are primarily very useful in competitive programming and also in various projects that require a specific technique with loops maintaining the overall structure of code. Â A lot of
6 min read
Use for Loop That Loops Over a Sequence in Python In this article, we are going to discuss how for loop is used to iterate over a sequence in Python. Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Using for loop we can iterate a sequence of elements
3 min read
Unpacking arguments in Python If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python. Unpacking without storing the values: You might encounter a situation where you
3 min read
Role of Underscores '_' in Python The Underscore (_) is an eccentric character in Python. It can be used in many ways in a Python program. The various uses of underscore (_) in Python are: 1) Use in Interpreter: Python immediately saves the value of the last expression in the interpreter in this unique variable. Underscore (_) can a
3 min read
Python - Loop Through a Range Looping through a range is an important operation in Python. In this article, we will explore the different ways to loop through a range in Python, demonstrating how we customize start, end, and step values, as well as alternative methods for more advanced use cases like looping through floating-poi
3 min read
Specifying the increment in for-loops in Python Let us see how to control the increment in for-loops in Python. We can do this by using the range() function. range() function range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where
2 min read
For loop in R For loop in R Programming Language is useful to iterate over the elements of a list, data frame, vector, matrix, or any other object. It means the for loop can be used to execute a group of statements repeatedly depending upon the number of elements in the object. It is an entry-controlled loop, in
5 min read
Python Do While Loops In Python, there is no construct defined for do while loop. Python loops only include for loop and while loop but we can modify the while loop to work as do while as in any other languages such as C++ and Java.In Python, we can simulate the behavior of a do-while loop using a while loop with a condi
6 min read