Unused variable in for loop in Python Last Updated : 01 Oct, 2020 Comments Improve Suggest changes Like Article Like Report 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=" ") Output* * * * * 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. Comment More infoAdvertise with us Next Article Unused variable in for loop in Python E erakshaya485 Follow Improve Article Tags : Python python-basics Practice Tags : python 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 How to Access Index using for Loop - Python When iterating through a list, string, or array in Python, it's often useful to access both the element and its index at the same time. Python offers several simple ways to achieve this within a for loop. In this article, we'll explore different methods to access indices while looping over a sequenc 2 min read How to Replace Values in a List in Python? Replacing values in a list in Python can be done by accessing specific indexes and using loops. In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in several ways. The simplest way to replace values in a list in Python is by using 2 min read Loops in Python - For, While and Nested Loops Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). Additionally, Nested Loops allow looping within loops for more complex tasks. While all the ways provide similar basic functionality, they differ in th 9 min read Python: Map VS For Loop Map in Python : Map is used to compute a function for different values 'in a single line of code ' . It takes two arguments, first is function name, that is defined already and the other is list, tuple or any other iterables . It is a way of applying same function for multiple numbers . It generates 3 min read Like