How to Return Multiple Values from a Python Function

Python how to return multiple values from a function

To return multiple values from a Python function, use a comma to separate the return values.

For instance, let’s create a function that takes two numbers as arguments. This function returns the sum, difference, multiplication, and division between these two numbers.

Here is how it looks in code:

def operate(a, b):
    sum = a + b
    diff = a - b
    mul = a * b
    div = a / b
    return sum, diff, mul, div

Now you can call this function for two numbers and assign the return values to variables:

n1 = 5
n2 = 10

sum, diff, mul, div = operate(n1, n2)

print(
    f"The sum is {sum}\n"
    f"The difference is {diff}\n"
    f"The multiplication gives {mul}\n"
    f"The division gives {div}\n"
)

This results in the following being printed in the console:

The sum is 15
The difference is -5
The multiplication gives 50
The division gives 0.5

How Does Returning Multiple Values Work in Python

In the previous section, you learned how to return multiple values by comma separating the values.

But why and how does it work?

Tuples in Python

The example code works because it returns a tuple.

In case you don’t know what a tuple is, check out this in-depth article. In short, a tuple is a group of zero or more elements.

For instance, here is an example of a tuple of three values that represents a 3D point:

coords = (1, 1, 3)

Notice that Python tuples do not always need parenthesis.

For example, you can write the above 3D point as:

coords = 1, 1, 3

This creates the same tuple of three values that represent a 3D point.

How to Access a Tuple Value in Python

To access and store a value from a tuple you can access it like you would access a value from a list. In other words, use the [] operator with an index.

For example, let’s store the 3D points into variables x, y, and z:

coords = 1, 1, 3

x = coords[0]
y = coords[1]
z = coords[2]

print(x, y, z)

This results in the following output in the console:

1 1 3

This works fine, but there is an easier way for this particular purpose.

Tuple Destructuring in Python

You can use tuple destructuring to access tuple values and store them into variables.

Tuple destructuring means you declare a bunch of comma-separated variables in one line and assign each tuple value to the corresponding variable. This saves you lines of code meanwhile it also makes the intention very clear.

Let me show what I mean

For instance, in the previous example, you stored 3D coordinates x, y, and z on separate lines using the [] operator. Instead of doing it this way, you can utilize tuple destructuring as a shorthand:

coords = 1, 1, 3

x, y, z = coords

print(x, y, z)

As a result, each coordinate in the 3D point is assigned into a separate variable.

1 1 3

As you can see, the first value of the tuple was attached to variable x, the second one to y, and the third one to z.

Now you understand how tuples are created and how values are read from them.

How to Return Multiple Values from a Function

Now that you understand the basics of tuples in Python, you can finally understand how to return multiple values from a function.

To return multiple values from a function, return the values as a tuple. To then access/store these values into variables, use the tuple destructuring.

If you now look at the example you saw in the introduction:

def operate(a, b):
    sum = a + b
    diff = a - b
    mul = a * b
    div = a / b
    return sum, diff, mul, div

You recognize this function returns a tuple of four values.

To access these four values, you use tuple destructuring:

n1 = 5
n2 = 10

sum, diff, mul, div = operate(n1, n2)

print(
    f"The sum is {sum}\n"
    f"The difference is {diff}\n"
    f"The multiplication gives {mul}\n"
    f"The division gives {div}\n"
)

Output:

The sum is 15
The difference is -5
The multiplication gives 50
The division gives 0.5

Conclusion

To return multiple values from a function in Python, return a tuple of values.

A tuple is a group of comma-separated values. You can create a tuple with or without parenthesis.

To access/store the multiple values returned by a function, use tuple destructuring.

Thanks for reading. Happy coding!

Further Reading

Python Tricks

How to Write to a File in Python

The with Statement in Python

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.