Return multiple values and only use One in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Return multiple values and only use One in Python
  2. Ignore multiple return values by accessing items at index

# Return multiple values and only use One in Python

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.

main.py
def my_func(): return ['a', 'b', 'c', 'd'] # โœ… by unpacking values _, _, c, _ = my_func() print(c) # ๐Ÿ‘‰๏ธ 'c' _, b, *_ = my_func() print(b) # ๐Ÿ‘‰๏ธ 'b'

return multiple values and only use one

The code for this article is available on GitHub
We used an underscore _ 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.

# Using the iterable unpacking operator

If you need to ignore items toward the end of the iterable, use the iterable unpacking operator.

main.py
def my_func(): return ['a', 'b', 'c', 'd'] _, b, *_ = my_func() print(b) # ๐Ÿ‘‰๏ธ 'b' print(_) # ๐Ÿ‘‰๏ธ ['c', 'd']

using iterable unpacking operator

The code for this article is available on GitHub

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.

main.py
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.

main.py
def my_func(): return ['a', 'b', 'c', 'd'] _, _, *rest = my_func() print(rest) # ๐Ÿ‘‰๏ธ ['c', 'd']

Alternatively, you can access the items you need directly.

# Ignore multiple return values by accessing items at index

To ignore multiple return values:

  1. Call the function and access the result at a specific index if you need to get a single value.
  2. Call the function and use slicing if you need to get multiple values.
main.py
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'
The code for this article is available on GitHub

If you need to get a specific item from the iterable, access the iterable at an index.

Python indexes are zero-based, so the first item in a list has an index of 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.

main.py
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].

The 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.

main.py
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.

main.py
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'
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev