The following is a Python function that takes two arguments, a and b, and returns six values.
def func1(a, b): r1, r2, r3, r4, r5, r6 = a+b, a*b, a//b, a**b, a-b, a/b return r1, r2, r3, r4, r5, r6 # Calling function func1 with a=6 and b=4 and printing the output print(func1(6, 4))
(10, 24, 1, 1296, 2, 1.5)
In Python, multiple values are, by default, returned as a tuple. In our case, therefore, we have a 6-element tuple as the output. In this article, we will discuss how to ignore some returned values if we are interested in a few of them. Let’s discuss two methods to do this:
- Using underscore (_) to ignore multiple return values
- Indexing the output to ignore some return values
In the following two sections, we will discuss these methods using the func1() function in the examples.
A standard convention is to use an underscore “_” as a variable name for the elements of the tuple we wish to ignore. For example, if we want to capture the second returned value only on our func1(), we will call the function as follows.
_, a2, _, _, _, _ = func1(6, 4) print(a2)
The variable a2 now contains the value 24 (for 6*4), and all other values are ignored. The variable “_” now captures the ignored. However, note that “_” is actually assigned a value for the last ignored value (1.5 for 6/4). You can check that by printing it.
Note: Using “_” as a throwaway variable is just a convention. Any other variable name can be used.
Suppose that our function was returning several values (like 100). Using “_” only to ignore return values can get messy because we will need at least 100 variables and underscores.
In this case, we need to asterisk (*) with “_” to ignore multiple values (* is used to unpack values from a container object). Here is an example of how we can pick the first return value and ignore all others. In this case, the underscore will store a tuple of all the ignored values.
a1, *_ = func1(6, 4) print(a1)
Ignoring all the return values except the first and the last
a1, *_, a6 = func1(6, 4) print(a1, a6) #Output: 10 1.5
Ignore all return values except the last two
*_, a5, a6 = func1(6, 4) print(a5, a6) #Output:
Suppose we want to ignore all return values at odd positions of our output. We cannot use *_ because using multiple starred expressions in assignments is prohibited. In that case, we need to use “_” for all odd positions as follows
_, a2, _, a4,_, a6 = func1(6, 4) print(a2, a4, a6)
24 1296 1.5
Once again, if several values are to be returned, using *_ or simply _ in a case like the above is not optimal. The second method will be a better choice.
In this case, the indexing operator ([ ]) is used to access return values that we cant to use and ignore the rest. For example, given a function func, func(<args>) picks the first element of the return tuple, func(<args>) returns the second value and so on.
Here are some examples using our function, func1(), defined at the beginning of the article.
#Pick the first value and ignore other return values a1 = func1(6, 4) print(a1)
# Pick the first two values of the return tuple a1, a2 = func1(6, 4)[0:2] print(a1, a2)
# Pick values at specific positions a3, a4, a6 = [func1(6, 4)[i] for i in [2, 3, 5]] print(a3, a4, a6)
1 1296 1.5
# Pick values at odd positions 1, 3 and 5 a2, a4, a6 = func1(6, 4)[1::2] print(a2, a4, a6)
24 1296 1.5
# Pick values at even positions 0, 2, 4 and 6 a1, a3, a5 = func1(6, 4)[0::2] print(a1, a3, a5)
10 1 2
There are two points to note from the above example. Firstly, note that Python uses zero-based indexing so that index 0 is at the first position, index 1 is at the second, and so on. That is why indices in [2, 3, 5] capture values at third, fourth, and sixth positions, respectively.
Secondly, Python allows indexing with steps. The general form of this indexing is <list>[start:stop:step]. The default values are start=0 (first element of <list>), stop=-1 (end of the list) and step=1.
In the last example, we picked the return values at the even positions of the returned tuple using [0::2], which is to say, start with 0 and end with the last element with a step size of 2. That captures the values at the indices: 0, 2, and 4 (first, third and fifth elements, respectively).
There are two ways to ignore return values in Python discussed in this article: using underscore (_) as a throwaway variable, indexing the values we want to use, and ignoring the rest.
In the first method, we also showed how to use “_” with an asterisk (*) to ignore multiple values. Each technique is best applied based on the used case.