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
Answer from A. Coady on Stack Overflow
🌐
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 ...
🌐
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
🌐
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.
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).

🌐
Programiz
programiz.com › python-programming › examples › multiple-return-values
Python Program to Return Multiple Values From a Function
To understand this example, you ... = name() print(name_1, name_2) Output · ('John', 'Armin') John Armin · When you return multiple values using comma(s), they are returned in the form of a tuple....
🌐
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 - Here’s how you’d write a function that returns a tuple. def person(): return "bob", 32, "boston" print(person()) # result: ('bob', 32, 'boston') Notice that we didn’t use parentheses in the return statement. That’s because you can return a tuple by separating each item with a comma, as shown in the above example.
🌐
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?
🌐
GeeksforGeeks
geeksforgeeks.org › python › g-fact-41-multiple-return-values-in-python
Returning Multiple Values in Python - GeeksforGeeks
Python automatically packs the values into a tuple, which can then be unpacked into variables. ... Explanation: fun() returns two values as a tuple, which are unpacked into s and x and then printed. Data class is a special type of class used ...
Published   July 1, 2025
Find elsewhere
🌐
Real Python
realpython.com › python-return-statement
The Python return Statement: Usage and Best Practices – Real Python
June 14, 2024 - The call to divmod() returns a tuple containing the quotient and remainder that result from dividing the two non-complex numbers provided as arguments. This is an example of a function with multiple return values.
🌐
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'.
🌐
Vultr
docs.vultr.com › python › examples › return-multiple-values-from-a-function
Python Program to Return Multiple Values From a Function | Vultr Docs
December 31, 2024 - Call the function and directly unpack the values into multiple variables. ... def get_stats(numbers): min_val = min(numbers) max_val = max(numbers) average_val = sum(numbers) / len(numbers) return min_val, max_val, average_val # Example usage stats = get_stats([10, 20, 30, 40, 50]) min_number, max_number, average_number = get_stats([10, 20, 30, 40, 50]) print(f"Min: {min_number}, Max: {max_number}, Avg: {average_number}") Explain Code
Top answer
1 of 8
435

You cannot return two values, but you can return a tuple or a list and unpack it after the call:

Copydef select_choice():
    ...
    return i, card  # or [i, card]

my_i, my_card = select_choice()

On line return i, card i, card means creating a tuple. You can also use parenthesis like return (i, card), but tuples are created by comma, so parens are not mandatory. But you can use parens to make your code more readable or to split the tuple over multiple lines. The same applies to line my_i, my_card = select_choice().

If you want to return more than two values, consider using a named tuple. It will allow the caller of the function to access fields of the returned value by name, which is more readable. You can still access items of the tuple by index. For example in Schema.loads method Marshmallow framework returns a UnmarshalResult which is a namedtuple. So you can do:

Copydata, errors = MySchema.loads(request.json())
if errors:
    ...

or

Copyresult = MySchema.loads(request.json())
if result.errors:
    ...
else:
    # use `result.data`

In other cases you may want to return a dict from your function:

Copydef select_choice():
    ...
    return {'i': i, 'card': card, 'other_field': other_field, ...}

But consider returning an instance of a utility class (or a Pydantic/dataclass model instance), which wraps your data:

Copyclass ChoiceData(pydantic.BaseModel):
    i: int
    card: str
    other_field: typing.Any

def select_choice():
    ...
    return ChoiceData(i=i, card=card, other_field=other_field)

choice_data = select_choice()
print(choice_data.i, choice_data.card)
2 of 8
32

I would like to return two values from a function in two separate variables.

What would you expect it to look like on the calling end? You can't write a = select_choice(); b = select_choice() because that would call the function twice.

Values aren't returned "in variables"; that's not how Python works. A function returns values (objects). A variable is just a name for a value in a given context. When you call a function and assign the return value somewhere, what you're doing is giving the received value a name in the calling context. The function doesn't put the value "into a variable" for you, the assignment does (never mind that the variable isn't "storage" for the value, but again, just a name).

When i tried to to use return i, card, it returns a tuple and this is not what i want.

Actually, it's exactly what you want. All you have to do is take the tuple apart again.

And i want to be able to use these values separately.

So just grab the values out of the tuple.

The easiest way to do this is by unpacking:

Copya, b = select_choice()
🌐
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?
def get_coordinates(): a = 10 b = 20 return a, b # Returning multiple values as a tuple # Example usage coordinates = get_coordinates() print(coordinates) # Output: (10, 20) print(coordinates[0]) # Access x print(coordinates[1]) # Access y · ...
🌐
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 ...
🌐
Linux Hint
linuxhint.com › return_multiple_values_python_function
Return Multiple Values from A Python Function – Linux Hint
Here, the tupleFunc() function is used to take four input values from the user and return the values as a tuple to the caller. The return values will be stored in a tuple variable named tupleVar and the values will be printed later. #!/usr/bin/env python3 # Define function to return multiple ...
🌐
Python Land
python.land › home › tips & tricks › python return multiple values
Python Return Multiple Values • Python Land Tips & Tricks
May 16, 2023 - Obviously, most functions in Python ... 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. ... All you need to do is list your values after the return statement, separated by commas. Here’s a runnable example of how to ...
🌐
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 - This corresponds to the value of “over_limit” in our function. We use the values[1] syntax to access the value of the average purchase. ... Our code works just like our last example, but this time we’ve used a list to separate the values in our code. We can return multiple values from a function using 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 …
🌐
W3Resource
w3resource.com › python-interview › how-do-you-return-values-from-a-function-in-python.php
Returning Values from Python Functions: Single and Multiple
... def get_max_min_value(nums): max_value = max(nums) min_value = min(nums) return [max_value, min_value] result_list = get_max_min_value([3, 9, 12, 27, 8]) print(result_list) # Output: [27, 3]