You can return any data type you want. Most of the time something like a tuple (eg. (404, "not found")) or a dict (eg. {"code": 404, "message": "not found"}) should work well. Sometimes, namedtuples or the more complex dataclasses are better to keep everything in order. Answer from D-K-BO on reddit.com
🌐
Real Python
realpython.com › python-return-statement
The Python return Statement: Usage and Best Practices – Real Python
June 14, 2024 - To return multiple values, list them after the return keyword separated by commas. Python packs these values into a tuple. You can then unpack the tuple into separate variables or store it as a single variable.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Return Multiple Values from a Function in Python | note.nkmk.me
April 23, 2025 - A tuple with one element requires a comma in Python · result = test() print(result) print(type(result)) # ('abc', 100) # <class 'tuple'> ... Each element in the returned tuple has the data type defined in the function. print(result[0]) print(type(result[0])) # abc # <class 'str'> print(result[1]) print(type(result[1])) # 100 # <class 'int'> ... Naturally, accessing an index beyond the number of returned values will raise an error.
Top answer
1 of 14
697

Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example.

>>> import collections
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(1, y=2)
>>> p.x, p.y
1 2
>>> p[0], p[1]
1 2

In recent versions of Python 3 (3.6+, I think), the new typing library got the NamedTuple class to make named tuples easier to create and more powerful. Inheriting from typing.NamedTuple lets you use docstrings, default values, and type annotations.

Example (From the docs):

class Employee(NamedTuple):  # inherit from typing.NamedTuple
    name: str
    id: int = 3  # default value

employee = Employee('Guido')
assert employee.id == 3
2 of 14
270

For small projects I find it easiest to work with tuples. When that gets too hard to manage (and not before) I start grouping things into logical structures, however I think your suggested use of dictionaries and ReturnValue objects is wrong (or too simplistic).

Returning a dictionary with keys "y0", "y1", "y2", etc. doesn't offer any advantage over tuples. Returning a ReturnValue instance with properties .y0, .y1, .y2, etc. doesn't offer any advantage over tuples either. You need to start naming things if you want to get anywhere, and you can do that using tuples anyway:

def get_image_data(filename):
    [snip]
    return size, (format, version, compression), (width,height)

size, type, dimensions = get_image_data(x)

IMHO, the only good technique beyond tuples is to return real objects with proper methods and properties, like you get from re.match() or open(file).

🌐
Flexiple
flexiple.com › python › python-return-multiple-values
Return multiple values from a function in Python | Flexiple Tutorials - Flexiple
Python basically uses a tuple to achieve this. ... #Returning Multiple Values using Tuples def multiple(): operation = "Sum" total = 5+10 return operation, total; operation, total = multiple() print(operation, total) #Output = Sum 15
🌐
Drbeane
drbeane.github.io › python › pages › functions › returning_multiple.html
Returning Multiple Values — Python for Data Science
def locate(x, item): index_list = [] for i in range(0, len(x)): if x[i] == item: index_list.append(i) return (index_list, len(index_list)) A list of student grades is provided in the cell below. Call locate() five times. In each function call, pass in grades for x. For item, use each of the following values: 'A', 'B', 'C', 'D', and 'F'.
Top answer
1 of 2
19

I think the choices need to be considered strictly from the caller's point of view: what is the consumer most likely to need to do?

And what are the salient features of each collection?

  • The tuple is accessed in order and immutable
  • The list is accessed in order and mutable
  • The dict is accessed by key

The list and tuple are equivalent for access, but the list is mutable. Well, that doesn't matter to me the caller if I'm going to immediately unpack the results:

score, top_player = play_round(players)
# or
idx, record = find_longest(records)

There's no reason here for me to care if it's a list or a tuple, and the tuple is simpler on both sides.

On the other hand, if the returned collection is going to be kept whole and used as a collection:

points = calculate_vertices(shape)
points.append(another_point)
# Make a new shape

then it might make sense for the return to be mutable. Homogeneity is also an important factor here. Say you've written a function to search a sequence for repeated patterns. The information I get back is the index in the sequence of the first instance of the pattern, the number of repeats, and the pattern itself. Those aren't the same kinds of thing. Even though I might keep the pieces together, there's no reason that I would want to mutate the collection. This is not a list.

Now for the dictionary.

the last one creates more readable code because you have named outputs

Yes, having keys for the fields makes heterogenous data more explicit, but it also comes with some encumbrance. Again, for the case of "I'm just going to unpack the stuff", this

round_results = play_round(players)
score, top_player = round_results["score"], round_results["top_player"]

(even if you avoid literal strings for the keys), is unnecessary busywork compared to the tuple version.

The question here is threefold: how complex is the collection, how long is the collection going to be kept together, and are we going to need to use this same kind of collection in a bunch of different places?

I'd suggest that a keyed-access return value starts making more sense than a tuple when there are more than about three members, and especially where there is nesting:

shape["transform"]["raw_matrix"][0, 1] 
# vs.
shape[2][4][0, 1]

That leads into the next question: is the collection going to leave this scope intact, somewhere away from the call that created it? Keyed access over there will absolutely help understandability.

The third question -- reuse -- points to a simple custom datatype as a fourth option that you didn't present.

Is the structure solely owned by this one function? Or are you creating the same dictionary layout in many places? Do many other parts of the program need to operate on this structure? A repeated dictionary layout should be factored out to a class. The bonus there is that you can attach behavior: maybe some of the functions operating on the data get encapsulated as methods.

A fifth good, lightweight, option is namedtuple(). This is in essence the immutable form of the dictionary return value.

2 of 2
1

Don't think about functions returning multiple arguments. Conceptually, it is best to think of functions as both receiving and returning a single argument. A function that appears to accept multiple arguments actually receives just a single argument of tuple (formally product) type. Similarly, a function that returns multiple arguments is simply returning a tuple.

In Python:

def func(a, b, c):
  return b, c

could be rewritten as

def func(my_triple):
  return (my_triple[1], my_triple[2])

to make the comparison obvious.

The first case is merely syntactic sugar for the latter; both receive a triple as an argument, but the first pattern-matches on its argument to perform automatic destructuring into its constituent components. Thus, even languages without full-on general pattern-matching admit some form of basic pattern matching on some of their types (Python admits pattern-matching on both product and record types).


To return to the question at hand: there is no single answer to your question, because it would be like asking "what should be the return type of an arbitrary function"? It depends on the function and the use case. And, incidentally, if the "multiple return values" are really independent, then they should probably be computed by separate functions.

🌐
freeCodeCamp
freecodecamp.org › news › python-returns-multiple-values-how-to-return-a-tuple-list-dictionary
Python Return Multiple Values – How to Return a Tuple, List, or Dictionary
July 20, 2020 - To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week. def miles_to_run(minimum_miles): week_1 = minimum_miles + 2 week_2 = minimum_miles + 4 week_3 = minimum_miles + 6 return [week_1, week_2, week_3] print(miles_to_run(2)) # result: [4, 6, 8] Data structures in Python are used to store collections of data, which can be returned from functions.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › g-fact-41-multiple-return-values-in-python
Returning Multiple Values in Python - GeeksforGeeks
It automatically creates useful methods like __init__() and __repr__(), so it's easy to create and return multiple values in a clean, readable way. ... from dataclasses import dataclass @dataclass class Student: name: str marks: int def get_student(): return Student("Alice", 85) student = get_student() print(student.name) print(student.marks)
Published   July 1, 2025
Top answer
1 of 9
119

Absolutely (for the example you provided).

Tuples are first class citizens in Python

There is a builtin function divmod() that does exactly that.

q, r = divmod(x, y) # ((x - x%y)/y, x%y) Invariant: div*y + mod == x

There are other examples: zip, enumerate, dict.items.

for i, e in enumerate([1, 3, 3]):
    print "index=%d, element=%s" % (i, e)

# reverse keys and values in a dictionary
d = dict((v, k) for k, v in adict.items()) # or 
d = dict(zip(adict.values(), adict.keys()))

BTW, parentheses are not necessary most of the time. Citation from Python Library Reference:

Tuples may be constructed in a number of ways:

  • Using a pair of parentheses to denote the empty tuple: ()
  • Using a trailing comma for a singleton tuple: a, or (a,)
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() built-in: tuple() or tuple(iterable)

Functions should serve single purpose

Therefore they should return a single object. In your case this object is a tuple. Consider tuple as an ad-hoc compound data structure. There are languages where almost every single function returns multiple values (list in Lisp).

Sometimes it is sufficient to return (x, y) instead of Point(x, y).

Named tuples

With the introduction of named tuples in Python 2.6 it is preferable in many cases to return named tuples instead of plain tuples.

>>> import collections
>>> Point = collections.namedtuple('Point', 'x y')
>>> x, y = Point(0, 1)
>>> p = Point(x, y)
>>> x, y, p
(0, 1, Point(x=0, y=1))
>>> p.x, p.y, p[0], p[1]
(0, 1, 0, 1)
>>> for i in p:
...   print(i)
...
0
1
2 of 9
28

Firstly, note that Python allows for the following (no need for the parenthesis):

q, r = divide(22, 7)

Regarding your question, there's no hard and fast rule either way. For simple (and usually contrived) examples, it may seem that it's always possible for a given function to have a single purpose, resulting in a single value. However, when using Python for real-world applications, you quickly run into many cases where returning multiple values is necessary, and results in cleaner code.

So, I'd say do whatever makes sense, and don't try to conform to an artificial convention. Python supports multiple return values, so use it when appropriate.

🌐
datagy
datagy.io › home › python posts › python: return multiple values from a function
Python: Return Multiple Values from a Function • datagy
December 19, 2022 - The way that this works, is that Python actually turns the values (separated by commas) into a tuple. We can see how this works by assigning the function to a variable and checking its type. # Returning Multiple Values with Tuples def return_multiple(): return 1, 2, 3 variable = return_multiple() print(type(variable)) # Returns: <class 'tuple'>
🌐
Career Karma
careerkarma.com › blog › python › python: return multiple values from a function
Python: Return Multiple Values from a Function | Career Karma
December 1, 2023 - You can return multiple values by bundling those values into a dictionary, tuple, or a list. These data types let you store multiple similar values. You can extract individual values from them in your main program.
🌐
Programiz
programiz.com › python-programming › examples › multiple-return-values
Python Program to Return Multiple Values From a Function
To understand this example, you ... get the individual items name_1, name_2 = name() print(name_1, name_2) ... When you return multiple values using comma(s), they are returned in the form of a tuple....
Top answer
1 of 7
15

It's not a sign of anything, and is not neither good nor bad design or coding style.

Returning multiple values can actually be appropriate and allow to write less code. Let's take an example of a method which takes a string like "-123abc" and converts it to an integer like -123:

(bool, int) ParseInteger(string text)
{
    // Code goes here.
}

returns both:

  • a value indicating whether the operation was a success,
  • the number converted from string.

How can we refactor this?

1. Exceptions

We can add exceptions, if the language supports them. Remember than in most languages, exceptions are expensive in resources. It means that if you have to deal with lots of non-numbers, it's better to avoid to throw an exception every time the string cannot be converted to a number.

2. New class

We can create a class and return an instance of an object of this class.

For example:

class ParsedInteger
{
    bool IsSuccess { get; set; }
    int Number { get; set; }
}

Is it easier to understand? Shorter to write? Does it bring anything? I don't think so.

3. Out parameters

If the language supports it, we can also use out parameters. This is the approach of C# where returning multiple values is not possible. For example, when parsing a number, we use: bool isSuccess = int.TryParse("-123abc", out i). I'm not sure how is it better to use out parameters compared to multiple values. The syntax is not obvious, and even StyleCop itself (the tool used to enforce the default Microsoft style rules on the code) complains about those parameters, suggesting to remove them when possible.


Finally, in languages as C# where there is no such a thing as returning multiple values, things are progressively added to imitate the behavior. For example, Tuple was added to allow returning several values without having to write your own class or use out parameters.

2 of 7
7

When your function returns a reference to an object that contains multiple members, is it returning one value or many? In the example you show, the function is actually returning an object of type tuple. Python just happens to support syntactic 'sugar' so that you don't have to explicitly dereference the members when making assignments from the return value of the function.

🌐
Embedded Inventor
embeddedinventor.com › home › python return multiple values
Python return Multiple Values
February 23, 2023 - >>> f = (1, (10, 20), (30, 40, ... values”, it is considered best practice to use braces whenever you use tuples to explicitly tell the reader that you are using tuples as this improves readability!...
🌐
Quora
quora.com › Is-returning-multiple-variables-in-Python-considered-poor-programming
Is returning multiple variables in Python considered 'poor' programming? - Quora
Answer (1 of 6): Nope; it is good programming. This question can be paraphrased as “Is returning a tuple in Python considered ‘poor’ programming?”, because that is exactly what those multiple values are - a tuple. Returning any data type cannot be considered bad practice.
🌐
TutorialsPoint
tutorialspoint.com › returning-multiple-values-in-python
Returning Multiple Values in Python?
For returning multiple values from a function, we can return tuple, list or dictionary object as per our requirement. def func(x): y0 = x+ 1 y1 = x * 3 y2 = y0 ** 3 return (y0, y1, y2) However, above program get problematic as the number of values returned increases.
🌐
Medium
martinxpn.medium.com › what-are-multiple-return-values-actually-in-python-28-100-days-of-python-82821c8de24b
What are Multiple Return Values Actually in Python? (28/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - In Python, it is possible to return multiple values from a function. This can be done by separating the values with a comma. The returned…
🌐
Python Land
python.land › home › tips & tricks › python return multiple values
Python Return Multiple Values • Python Land Tips & Tricks
May 16, 2023 - 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.
🌐
Python.org
discuss.python.org › python help
How do you return multiple variables in a function? - Python Help - Discussions on Python.org
December 4, 2021 - I’ve been trying to write a piece of code where 2 variables defined in the first function are returned and used in the second function. I’ve tried returning the 2 variables with a comma between them but I keep getting a …