Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Returning Multiple Values With namedtuple

For more information on topics covered in this lesson, check out these resources:

00:00 In a previous lesson, you saw how to return multiple values in a single return statement. Here, we’ll look at a way to make those types of functions more readable.

00:11 In Python, you can use namedtuples in a function with multiple return values to essentially put a label on each value, making your function more understandable to other programmers.

00:23 Officially, a namedtuple is an object of the collections.namedtuple class. A namedtuple is a collection class which returns a subclass of tuple that has fields or attributes.

00:37 You can access those individual attributes using either dot notation or an indexing operation. For more information about namedtuple, you can check out Write Pythonic and Clean Code with namedtuple here on Real Python.

00:53 The initializer of namedtuple takes several arguments, but you only really need to know two of them to get started. The first is typename.

01:03 This is the name of the class you wish to use for your namedtuple. It has to be a string. The second argument is field_names. This holds the names of the fields or attributes your tuple-like object is going to have.

01:20 It can be a sequence of strings or a single string with names separated by whitespace or commas. Here’s an example. It’s a new version of the describe() method you saw in a previous lesson.

01:35 First are some import statements. As before, we need to import statistics to use the statistical functions. And to use the namedtuple, we need to import the namedtuple module from collections.

01:48 Again, our function describe() will take a collection of data for an argument, which we’re calling sample. Now we create our namedtuple class.

01:59 We’ll call this class object Desc, which will match the typename we give to the namedtuple initializer. This isn’t a requirement, but it’s considered proper Python style.

02:12 Now we call the initializer for namedtuple. We see the string "Desc", which is the typename, and in the second argument, we see names for the fields, "mean", "median", and "mode".

02:26 In our return statement, we’re still returning those three values, but instead of in an ordinary tuple, we’ll make them an object of our new Desc namedtuple class.

02:38 The initializer for Desc requires three values, one for each of the attributes we named. In this case, each one will have the result of a different measure of center for the data in sample.

02:53 First will be the mean, then the median, and finally, the mode. These are put in a namedtuple object of type Desc and returned.

03:06 Now let’s look at how to use this returned object. First, let’s import it and create a set of data to use.

03:17 I’m hoping this is the same data we used in that previous lesson. And now call our describe() function on this data and save it to a variable.

03:31 Now let’s explore the various ways we can see the values this function computed. We can display the value of the entire object.

03:41 We can access an individual attribute using its name with the dot operator (.)

03:48 or by an index number

03:53 for the median. You can also unpack the tuple when you call the function.

04:05 So now, each variable holds a single value.

04:12 Again, by returning an object with the attribute names, our function becomes more readable and easier for other programmers to use. This concludes our series of lessons on best practices using the return statement.

04:26 Next will be some lessons that show some of the more advanced features Python provides for returning objects.

Become a Member to join the conversation.