What does `variable, _, _ = ...` mean in Python?
variable, _, _ = ... mean in python? with practical examples, diagrams, and best practices. Covers python development techniques with visual explanations.Categories:
Understanding Python's Placeholder Variable: variable, _, _ = ...

Explore the common Python idiom of using underscores as placeholder variables in tuple unpacking, function returns, and loop iterations to improve code readability and intent.
In Python, you often encounter situations where a function returns multiple values, or you need to iterate over a sequence but only care about a subset of the elements. This is where the _ (underscore) character comes into play as a placeholder variable. While _ is a valid identifier, its conventional use signals to other developers (and often to linters) that the assigned value is intentionally being ignored. This article delves into the various contexts where variable, _, _ = ... and similar patterns are used, explaining their meaning and best practices.
Tuple Unpacking and Ignoring Values
One of the most common scenarios for using _ is during tuple unpacking (or sequence unpacking in general). When a function returns a tuple of multiple values, but you only need one or a few of them, you can use _ to explicitly ignore the values you don't intend to use. This makes your code cleaner and communicates your intent clearly.
def get_user_info():
# Imagine this fetches user ID, username, email, and last login timestamp
return 101, "alice", "alice@example.com", "2023-10-26 10:30:00"
# We only care about the username
_, username, _, _ = get_user_info()
print(f"Username: {username}")
# Another example: getting width and height from a bounding box, ignoring x, y
x, y, width, height = (10, 20, 100, 150)
_, _, w, h = (10, 20, 100, 150)
print(f"Width: {w}, Height: {h}")
Using underscores to ignore unwanted values during tuple unpacking.
* with _ (e.g., first, *_, last = sequence). This is known as 'starred assignment' or 'extended iterable unpacking' and is available in Python 3+.Looping and Iteration with Placeholders
Similar to tuple unpacking, _ is frequently used in for loops when the loop variable itself is not needed within the loop's body. This often happens when you want to perform an action a fixed number of times, or when iterating over a sequence where only side effects or specific conditions matter, not the iterated item itself.
# Perform an action 5 times
for _ in range(5):
print("Hello!")
# Iterating over a list of tuples, only interested in the second element
data = [('apple', 10), ('banana', 20), ('cherry', 30)]
for _, quantity in data:
print(f"Quantity: {quantity}")
Using _ in for loops when the loop variable is not used.
The Single Underscore vs. Double Underscore (__)
It's important to distinguish between the single underscore _ and the double underscore __. While _ is a convention for ignored variables, __ (double underscore) has a special meaning in Python. It's used for 'name mangling' in classes to make attributes effectively private, preventing accidental name clashes in subclasses. It's not typically used as a placeholder for ignored values.
class MyClass:
def __init__(self):
self.public_var = 10
self._protected_var = 20 # Convention for 'protected'
self.__private_var = 30 # Name mangled to _MyClass__private_var
obj = MyClass()
print(obj.public_var)
# print(obj.__private_var) # This would raise an AttributeError
print(obj._MyClass__private_var) # Accessing the mangled name (not recommended)
Demonstrating the difference between _ and __ in Python.
_ is a convention, Python does not strictly enforce it. You can still access the value assigned to _ if you need to, but doing so goes against the established convention and can reduce code clarity. Some linters might warn you if you use _ and then try to access its value.
Decision flow for using the underscore placeholder in Python.