Last updated: Apr 9, 2024
Reading timeยท3 min
Use unpacking with placeholder variables to ignore multiple return values,
e.g. _, _, c, _ = my_func()
.
The variables on the left need to be exactly as many as the values in the iterable on the right.
def my_func(): return ['a', 'b', 'c', 'd'] # โ by unpacking values _, _, c, _ = my_func() print(c) # ๐๏ธ 'c' _, b, *_ = my_func() print(b) # ๐๏ธ 'b'
_
for the name of the variables. There is a convention to use an underscore as the name of placeholder variables we don't intend to use.The variables on the left need to be exactly as many as the values in the iterable on the right.
This is because we are unpacking the values of the iterable into variables.
If you need to ignore items toward the end of the iterable, use the iterable unpacking operator.
def my_func(): return ['a', 'b', 'c', 'd'] _, b, *_ = my_func() print(b) # ๐๏ธ 'b' print(_) # ๐๏ธ ['c', 'd']
The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
The *
operator can be used to ignore an arbitrary number of return values.
def my_func(): return ['a', 'b', 'c', 'd'] _, *_, c, d = my_func() print(c) # ๐๏ธ 'c' print(d) # ๐๏ธ 'd'
However, note that only one unpack operation can be used. In other words, you
can only use *_
once on the left-hand side.
If you use the unpacking operator to store values you intend to use in your code, name the variable accordingly.
def my_func(): return ['a', 'b', 'c', 'd'] _, _, *rest = my_func() print(rest) # ๐๏ธ ['c', 'd']
Alternatively, you can access the items you need directly.
To ignore multiple return values:
def my_func(): return ['a', 'b', 'c', 'd'] c = my_func()[2] print(c) # ๐๏ธ 'c' d = my_func()[3] print(d) # ๐๏ธ 'd' c, d = my_func()[2:4] print(c) # ๐๏ธ 'c' print(d) # ๐๏ธ 'd' # -------------------------------- result = my_func() c = result[2] print(c) # ๐๏ธ 'c' d = result[3] print(d) # ๐๏ธ 'd'
If you need to get a specific item from the iterable, access the iterable at an index.
0
, and the last item has an index of -1
or len(my_list) - 1
.You can use list slicing if you need to get a slice of the iterable.
def my_func(): return ['a', 'b', 'c', 'd'] c, d = my_func()[2:4] print(c) # ๐๏ธ 'c' print(d) # ๐๏ธ 'd'
The syntax for list slicing is my_list[start:stop:step]
.
start
index is inclusive and the stop
index is exclusive (up to, but not including).However, calling the function multiple times is not as efficient as calling the function once and storing the result in a variable.
def my_func(): return ['a', 'b', 'c', 'd'] result = my_func() c = result[2] print(c) # ๐๏ธ 'c' d = result[3] print(d) # ๐๏ธ 'd' c, d = result[2:4] print(c) # ๐๏ธ 'c' print(d) # ๐๏ธ 'd'
We stored the result of calling the function in a variable and accessed the list multiple times.
If you use NumPy, you can return a NumPy array from the iterable.
import numpy as np def my_func(): return np.array(['a', 'b', 'c', 'd']) a, d = my_func()[[0, 3]] print(a) # ๐๏ธ 'a' print(d) # ๐๏ธ 'd'
NumPy arrays can be accessed at multiple, non-consecutive indexes.
Notice that we used two sets of square brackets.
I've also written an article on how to return a default value if None.
You can learn more about the related topics by checking out the following tutorials: