How to Ignore Return Values in Python
When calling a Python function that returns multiple values, you often only need a subset of those values.
This guide explains how to selectively ignore return values using unpacking with the underscore (_
) convention and the starred expression (*
) for handling an arbitrary number of ignored values. We also cover an alternative for when unpacking is not possible, by accessing the values directly.
Ignoring Values with Unpacking and _
The most Pythonic and readable way to ignore specific return values is to use unpacking with the underscore (_
) as a placeholder:
def my_func():
return ['a', 'b', 'c', 'd']
_, _, c, _ = my_func() # Ignore the first, second and fourth values
print(c) # Output: c
- Unpacking: The line
_, _, c, _ = my_func()
unpacks the list returned bymy_func()
into four variables. - Underscore (
_
): By convention, the underscore (_
) is used as a variable name to indicate that you don't intend to use that value. It's a signal to other programmers (and to yourself) that the value is being deliberately ignored. You can use as many underscores as necessary. - You must provide the same amount of parameters to the left, as there are returned values to the right.
Ignoring Multiple Values with *
(Starred Expression)
If you need to ignore a variable number of values at the beginning, middle, or end of the returned sequence, use a starred expression (*
) with unpacking:
def my_func():
return ['a', 'b', 'c', 'd']
_, b, *_ = my_func() # Ignore the first value and all after 'b'
print(b) # Output: b
print(_) # Output: ['c', 'd']
_, *_, c, d = my_func() # Ignore values before the last two
print(c) # Output: c
print(d) # Output: d
_, b, *_ = my_func()
: The*
before_
collects all the remaining values (after assigning tob
) into a list, which is then assigned to_
. Because we're using_
, we're indicating that we don't intend to use this list._, *_, c, d = my_func()
: The first returned element is ignored, all of the elements until the last two are ignored, and those last two are assigned toc
andd
.- Only one unpack operation can be used on the left side.
Ignoring Values with Indexing/Slicing (Less Recommended)
You can access specific return values by indexing or slicing the result of the function call, but this is generally less readable than unpacking:
def my_func():
return ['a', 'b', 'c', 'd']
c = my_func()[2] # Access the third element directly
print(c) # Output: c
c, d = my_func()[2:4] # Get a slice, then unpack
print(c) # Output: c
print(d) # Output: d
my_func()[2]
is accessing the value at a specific index, in this case it is2
.- This is less readable because you are not explicitly indicating which elements you're ignoring.
It is preferred to store the returned value into a variable before accessing it:
def my_func():
return ['a', 'b', 'c', 'd']
result = my_func()
c = result[2]
print(c) # Output: c
d = result[3]
print(d) # Output: d
c, d = result[2:4]
print(c) # Output: c
print(d) # Output: d
The approach of calling the function multiple times is not efficient, because all of the code inside the function is executed on every call.