Understanding Throw-away Variables in Python
When learning Python, you may come across code like this:
for _ in range(5):
print("Hello")
Many beginners wonder:
What is
_? Why not usei?
The answer is simple. _ is called a throw-away variable.
What is a Throw-away Variable?
A throw-away variable is a variable whose value is intentionally ignored.
Sometimes Python gives us a value, but we don't actually need it. Instead of creating a meaningful variable name, we use _ to show that the value is not important.
For example:
for _ in range(5):
print("Hello")
Here, range(5) generates these values:
0, 1, 2, 3, 4
But we don't use any of these values. We only want the loop to run 5 times.
That's why we use _.
Why Use _ Instead of i?
Both of the following programs work exactly the same.
Using i:
for i in range(5):
print("Hello")
Using _:
for _ in range(5):
print("Hello")
The difference is readability.
Using i makes it look like the loop variable might be used later.
Using _ clearly tells anyone reading the code:
"This value is intentionally ignored."
Common Uses of Throw-away Variables
1. Ignoring the loop counter
for _ in range(3):
print("Python")
Output:
Python
Python
Python
2. Ignoring values while unpacking
name, _, age = ("Alice", "Engineer", 25)
print(name)
print(age)
Output:
Alice
25
The value "Engineer" is ignored because we don't need it.
3. Ignoring multiple values
first, *_, last = [10, 20, 30, 40, 50]
print(first)
print(last)
Output:
10
50
The middle values are ignored.
Real-world Use Case: Codeforces Bit++ (B++) Problem
Now let's see a practical example where a throw-away variable makes the code cleaner.
The Bit++ problem gives us n statements.
Each statement is one of the following:
X++
++X
X--
--X
Initially,
X = 0
For every statement:
X++or++XincreasesXby1X--or--XdecreasesXby1
Our task is to print the final value of X.
Python Solution
n = int(input())
x = 0
for _ in range(n):
statement = input()
if '+' in statement:
x += 1
else:
x -= 1
print(x)
Why _ is Used Here
Let's focus on this line:
for _ in range(n):
range(n) generates numbers like:
0, 1, 2, 3, ...
But do we ever use these numbers?
No.
We only need the loop to execute n times so that we can read n input statements.
Since the loop variable is never used, _ is the perfect choice.
We could also write:
for i in range(n):
This would produce the same output.
However, since i is never used, writing _ makes our intention clear:
"I don't care about this value. I only need the loop to repeat."
This small convention makes Python code cleaner and easier to understand.
Key Takeaways
_is not a special keyword in Python.- It is a normal variable name used by convention.
- It tells other programmers that a value is intentionally ignored.
- It is commonly used in:
- Loops when the loop counter isn't needed.
- Tuple or list unpacking to ignore unwanted values.
- Extended unpacking (
*_) to ignore multiple values.
Using _ makes your code more readable and more Pythonic.
The next time you see code like this:
for _ in range(10):
or
name, _, age = data
you'll immediately know that _ simply means:
"This value exists, but I don't need it."
