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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ g-fact-41-multiple-return-values-in-python
Returning Multiple Values in Python - GeeksforGeeks
Explanation: fun() creates a dictionary ... a class can hold multiple values as attributes. A function can return an object to group and return several related values together....
Published ย  July 1, 2025
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_variables_multiple.asp
Python Variables - Assign Multiple Values
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Note: Make sure the number of variables matches the number of values, or else you will get an error. And you can assign the same value to multiple variables in one line:
Discussions

return - Alternatives for returning multiple values from a Python function - Stack Overflow
The canonical way to return multiple values in languages that support it is often tupling. Option: Using a tuple Consider this trivial example: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you return multiple variables in a function?
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 Type Error. I would really appreciate if someone told me how to fix ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
December 4, 2021
language comparison - What are the different ways of handling multiple return values? - Programming Language Design and Implementation Stack Exchange
(inspired by this question) Different programming languages handle multiple return values differently, used in functions such as divmod. For example, in Python or Haskell, you would just return a t... More on langdev.stackexchange.com
๐ŸŒ langdev.stackexchange.com
May 19, 2023
How to call a Python function that returns multiple values with Python Interface
I have a Python function that returns multiple values. How can I call my Python function to return multiple values? More on mathworks.com
๐ŸŒ mathworks.com
1
0
August 22, 2023
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Return Multiple Values from a Function in Python | note.nkmk.me
April 23, 2025 - The same applies to three or more return values. def test2(): return 'abc', 100, [0, 1, 2] a, b, c = test2() print(a) # abc print(b) # 100 print(c) # [0, 1, 2] ... By using [], you can return a list instead of a tuple. def test_list(): return ['abc', 100] result = test_list() print(result) print(type(result)) # ['abc', 100] # <class 'list'> ... Draw circle, rectangle, line, etc. with Python, Pillow
๐ŸŒ
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.
๐ŸŒ
DataCamp
campus.datacamp.com โ€บ courses โ€บ introduction-to-functions-in-python โ€บ writing-your-own-functions
Multiple parameters and return values | Python
This means that when we call raise_to_power(2, 3), when the function is executed, 2 would be assigned to value1 and 3 to value2. Looking at the function body, this means that the computation value1 to the power of value2 translates to 2 to the power of 3. This function call then returns the value 8. You can also make your function return multiple values.
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).

Find elsewhere
๐ŸŒ
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 โ€ฆ
๐ŸŒ
QASource
blog.qasource.com โ€บ software-development-and-qa-tips โ€บ how-to-return-multiple-values-in-python
How To Return Multiple Values From a Function in Python?
In summary, you can return multiple values in Python using tuples, lists, dictionaries, objects or namedtuples, generators, or multiple return statements.
๐ŸŒ
Boot.dev
boot.dev โ€บ lessons โ€บ 3c5fe40f-41e3-4d7e-a035-be67c8d83536
Learn to Code in Python: Multiple Return Values | Boot.dev
def cast_iceblast(wizard_level, start_mana): damage = wizard_level * 2 new_mana = start_mana - 10 return damage, new_mana # return two values ยท When calling a function that returns multiple values, you can assign them to multiple variables.
๐ŸŒ
Medium
medium.com โ€บ @1511425435311 โ€บ multiple-parameters-and-return-values-d241100e76f6
Exploring Multiple Parameters and Return Values in Python ๐Ÿ | by Miguel de la Vega | Medium
December 29, 2023 - By specifying the index, you can ... of the tuple using zero-indexing. ... Python functions can return multiple values by using tuples....
๐ŸŒ
Medium
medium.com โ€บ opsops โ€บ user-friendly-way-to-return-multiple-values-from-a-function-in-python-9e5554d66273
User-friendly way to return multiple values from a function in Python | by George Shuklin | OpsOps | Medium
November 27, 2020 - Now we got names for return values. In exchange caller need to suffer: ... My solution is to use class as a data holder. Weโ€™ll ignore all fancy things we can do, and keep code as simple as possible: def foo(): class retval: odd = 1 even = 2 string_example = 'foo' return retval
๐ŸŒ
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'.
๐ŸŒ
Python Basics
pythonbasics.org โ€บ multiple-return
Multiple return - Python Tutorial
In that case you can return variables from a function. In the most simple case you can return a single variable: Call the function with complexfunction(2,3) and its output can be used or saved. But what if you have multiple variables in a function that you want access to?
๐ŸŒ
W3Resource
w3resource.com โ€บ python-interview โ€บ how-do-you-return-values-from-a-function-in-python.php
Returning Values from Python Functions: Single and Multiple
August 12, 2023 - To return a single value from a function, we can simply use the return statement followed by the value you want to return. ... We can return multiple values from a function as a tuple.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 2030984-how-to-call-a-python-function-that-returns-multiple-values-with-python-interface
How to call a Python function that returns multiple values with Python Interface - MATLAB Answers - MATLAB Central
August 22, 2023 - When a Python function returns multiple values, these values will be returned to MATLAB as a single Python tuple object. To retrieve individual values, you will need to unwrap the tuple.
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.

๐ŸŒ
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.