Python Return Multiple Values

With Python, we can return multiple values at once. Obviously, most functions in Python return a single value, usually the result of the work done by that function. In this article, you’ll learn that you can return multiple values in Python too, and you don’t need a dictionary, list, or a data class to do so.

Return multiple values with a tuple

All you need to do is list your values after the return statement, separated by commas. Here’s an example of how to return multiple values and how to assign them to multiple variables at once:

def get_user(id):
    # fetch user from database
    # ....
    return name, birthdate

name, birthdate = get_user(4)

What we are actually doing here, is returning a tuple. We could have written return (name, birthdate) as well, with the same effect. And if you’re wondering whether you can return more than two values this way: yes you can!

The most obvious advantage of returning multiple values in Python using a tuple is that we can directly assign the result to multiple variables. It’s super clean and concise, just like Python was meant to be.

Alternative ways in Python to return multiple values

Using tuples to return multiple values in Python is alright for a limited number of values. Because we can directly unpack them into clearly named variables, it actually results in very readable, concise code.

However, once you get past 2 or 3 values, you should look into a data class to keep your code clean and readable. A data class is essentially a regular class, annotated with @dataclass to give it some handy extra functionality.

If a data class is not an option because you’re running an older Python version, for example, you can also consider returning:

What’s best depends on the kind of data you’re returning. If the data fits in a list nicely, use a list. In case the data has keys and values, use a dictionary. If your data is more complex, you might even need to put multiple lists inside a dictionary. Just remember, at some point, it will be cleaner to use a (data) class.

Keep learning

Please share this article with others!

Python Courses

Are you enjoying this free tutorial? Please also have a look at my premium courses. They offer a superior user experience with small, easy-to-digest lessons and topics, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

The Python Fundamentals Course For Beginners
Now for $29 (from $59)

Python Fundamentals I is a course for beginners that will get you started with Python in no time. Learn all the essentials, test your progress with quizzes and assignments, and bring it all together with the final course project!

Python Course for Beginners

Modules, Packages, And Virtual Environments
Now for $29 (from $59)

Python Fundamentals II covers creating your own modules and packages, using virtual environments and Python package managers to make your life as a programmer easier. Advance your productivity as a Python programmer!

Python Fundamentals 2

Leave a Comment

Share to...