Return Multiple Values From a Function
00:00 As the last item on the agenda for tuples, I want to show you a place where you may have used tuples implicitly without knowing about it. And that is how you can return multiple values from a function, which isn’t quite the right way to say this, but I’ll show you why in just a second.
00:19
In Python, you can define a function. Let’s say we’re going to call this one get_stats()
, and it’ll take as an argument, a sequence, and then it will return the maximum of that sequence and the minimum of that sequence.
00:36
There are two built-in functions that do that in Python. So there’s the max()
function, and I can pass it the sequence,
00:44
and the min()
function. Now, I’m not going to return only a single value, but I’m going to add a comma here and then say min(sequence)
.
00:52 So it looks like I’m returning two values here. It looks like I’m returning the maximum of the sequence and the minimum of the sequence, and I am in a way.
01:00 But after going through this course, you’ll know that what you’re actually doing here is creating a tuple literal. So you could wrap this inside of parentheses as well, and it would be the same thing.
01:09 So you’re creating a tuple literal, and then you’re returning the tuple literal with these two elements in it. One of the elements is going to be the maximum of the sequence, and the other element is going to be the minimum of the sequence.
01:21
However, in a return
statement of a Python function, you very often are not going to see people putting the tuple parentheses, but instead they just write out the values they want to return with a comma.
01:34 This is a common way that you may see the definition of a function where the person wants to return multiple variables. What they really do is return a single tuple that contains the values that they want to return.
01:47
So now I defined get_stats()
. Let’s try it out on the numbers
01:52
list that we’ve worked with before. It’s not going to be very interesting, but it is enough for our demonstration purposes here. So I can say get_stats()
, and then pass in the numbers
sequence.
02:05 There’s also a tuple in that case. Could be a different sequence as well. If I press Enter, you can see that what Python returns is a tuple with first the maximum value and then the minimum value.
02:18 Let’s confirm that this is a tuple. The return value
02:23 of calling this function is indeed a tuple.
02:27 And what you can do and how this “multiple return values,” in quotes, I guess, is often used is that you combine it with tuple unpacking.
02:38
Let’s say you want to get the maximum and the minimum and work with those two values from some sort of a sequence. Then you can use this get_stats()
function to do that and combine it with tuple unpacking to get access to both of those values separately.
02:53
I could do it like this, that I say maximum, minimum =
02:59
and then I’m going to call the get_stats()
function and pass it the sequence. In this case, this is our tuple numbers
. And now what happens here is that get_stats()
takes numbers
as an argument, figures out what’s the maximum and the minimum, then puts these two into a tuple literal and returns that tuple.
03:19
And then out here we’re unpacking the return value of this function into the two values that it returns. And you’re going to do tuple unpacking to assign the first value to the variable maximum
and the second value to the variable minimum
.
03:34
So now you have access to maximum
, which points to 3
in this case, and minimum
which points to 1
. That’s kind of interesting and a relatively common use case of, I would say, hidden tuples that you will find in Python because, like I mentioned before, it’s rare that someone actually returns the tuple in a more visible way.
03:55 By wrapping these two return values into additional parentheses, you can do it. It’s got the same effect. I would probably even suggest you to do it because I think it’s a little more obvious what’s happening and takes away this idea that it’s possible to return multiple values from a function.
04:13 It’s not. You’re always going to return just one value, but you can wrap multiple values inside of a collection and then return that collection, which is what you’re doing in this case.
04:26 So you can use tuples to return multiple values from a function. Really, you’re just returning the tuple, but the values are wrapped in there. And then you can use tuple unpacking to access those individual values from the tuple.
04:41 And this wraps up our walk-through over the tuple data type in Python. They’re immutable sequences. And next you’re going to start looking at mutable sequences, which are lists in Python.
04:53 You’ll see that they are quite similar in some ways to tuples, but they have a notable difference, which is that they’re mutable, which makes them a lot more flexible.
05:01 You’ll see that lists are used for a much wider variety of use cases than tuples. All right, in the next section, let’s start taking a look at lists. But before that, I have a few review exercises for you that you can try out to fortify the things that you’ve learned about in this section.
Become a Member to join the conversation.