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
๐ŸŒ
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 ...
Published ย  July 1, 2025
๐ŸŒ
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]) ...
๐ŸŒ
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.
๐ŸŒ
Real Python
realpython.com โ€บ python-return-statement
The Python return Statement: Usage and Best Practices โ€“ Real Python
June 14, 2024 - Note that, to return multiple values, you just need to write them in a comma-separated list in the order you want them returned. Note: If your functions needs several different return types, then youโ€™re dealing with a more complex scenario.
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).

๐ŸŒ
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.
๐ŸŒ
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 โ€ฆ
Find elsewhere
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()
๐ŸŒ
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'.
๐ŸŒ
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
๐ŸŒ
Python Basics
pythonbasics.org โ€บ multiple-return
Multiple return - Python Tutorial
Create a function getPerson(). As you already know a function can return a single variable, but it can also return multiple variables.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ return_multiple_values_python_function
Return Multiple Values from A Python Function โ€“ Linux Hint
This is another option to return many values from a function. A dictionary object variable named dictVar is declared inside the function. Three values are assigned to the variable and return the dicVar to the caller. Next, the dictionary values are printed. #!/usr/bin/env python3 # Define function ...
๐ŸŒ
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'>
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ python โ€บ python function return multiple values [solved]
Python function Return Multiple Values [SOLVED] | GoLinuxCloud
January 9, 2024 - In this example, the add function ... in the variable result. Python allows you to return multiple values by separating return values with commas....
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ multiple-return-values
Python Program to Return Multiple Values From a Function
Python Functions ยท def name(): ... = 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....
๐ŸŒ
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 - Define a function that evaluates conditions and returns different sets of values depending on the outcome. Use the returned multiple values based on the condition.
๐ŸŒ
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.
๐ŸŒ
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 - User-friendly way to return multiple values from a function in Python assert foo().bar in foobar().baz Personally, I think that dictionary syntax for Python is the least well done thing in Python โ€ฆ