Ignore Multiple Return Value in Python

The following is a Python function that takes two arguments, a and b, and returns six values.

Output:

(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:

  1. Using underscore (_) to ignore multiple return values
  2. 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.

Method 1: Using underscore (_) to ignore multiple values

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.

Output:

24

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.

Output:

10

More examples:

Ignoring all the return values except the first and the last

Ignore all return values except the last two

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

Output:

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.

Method 2: Indexing to ignore some return values

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>)[0] picks the first element of the return tuple, func(<args>)[1] returns the second value and so on.

Here are some examples using our function, func1(), defined at the beginning of the article.

Output:

10

Output:

10 24

Output:

1 1296 1.5

Output:

24 1296 1.5

Output:

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 the 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).

Conclusion

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.