Unused variable in for loop in Python
Last Updated :
01 Oct, 2020
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 variable
for i in range(0, 5):
x = (i+1)*2
print(x, end=" ")
# loop-2
print("\nUsing the loop variable only for iteration :")
# unsused loop variable
for j in range(0, 5):
print('*', end=" ")
OutputUsing the loop variable inside :
2 4 6 8 10
Using the loop variable only for iteration :
* * * * *
In the code snippet above, in loop-1, the loop control variable 'i' is used inside the loop for computation. But in loop-2, the loop control variable 'j' is concerned only with keeping tracking of the iteration number. Thus, 'j' is an unused variable in for loop. It is good practice to avoid declaring variables that are of no use. Some IDEs like Pycharm, PyDev, VSCode produce warning messages for such unused variables in the looping structure. The warning may look like something given below:
To avoid such warnings, the convention of naming the unused variable with an underscore('_') alone can be used. This avoids the problem of unused variables in for loops. Consider the following script with an unused loop variable tested with the Vulture module in Python. Once the vulture module is installed using the pip command, it can be used for testing .py scripts in the anaconda prompt.
Example: trial1.py
Python3
# unused function
def my_func():
# unused local variable
a = 5
b = 2
c = b+2
print(b, c)
# unused loop variable 'i'
for i in range(0, 5):
print("*", end=" ")
Checking with vulture module
To avoid this unused variable 'i' warning, the loop variable can simply be replaced by an underscore ('_'). Look at the code snippet below
Python3
# unused function
def my_func():
b = 2
c = b+2
print(b, c)
# unused loop variable 'i'
for _ in range(0, 5):
print("*", end=" ")
Some use pylint, a tool to keep track of code styles and dead code in Python. Such a tool can raise a warning when the variable in for loop is not used. To suppress that, it is better to use the underscore naming conventions for the unused variables.
Similar Reads
Unused local variable in Python A variable defined inside a function block or a looping block loses its scope outside that block is called ad local variable to that block. A local variable cannot be accessed outside the block it is defined. Example: Python3 # simple display function def func(num): # local variable a = num print(
4 min read
Understanding for-loop in Python 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
6 min read
Decrement in While Loop in Python A loop is an iterative control structure capable of directing the flow of the program based on the authenticity of a condition. Such structures are required for the automation of tasks. There are 2 types of loops presenting the Python programming language, which are: for loopwhile loop This article
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
How to Skip a Value in a List in Python Skipping a value in a list means we want to exclude certain items while iterating. There are many ways to skip a value in Python List. Using continue is the simplest way to skip a value in iteration, without modifying the original list. Pythona = [1, 2, 3, 4, 5] # iterating the list for i in a: if i
2 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