Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Python Land
  • Python Tutorial
  • Courses
  • Blog
  • Contact
  • Log in
Home » Tips & Tricks » Python Return Multiple Values
A boomerang flies back to a get_user() function carrying the tuple ("Ada", 36) from return name, age

Python Return Multiple Values

By Erik van Baaren · Published December 20, 2020 · Updated November 10, 2026

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.

Table of Contents

  • 1. Return multiple values with a tuple
  • 2. Alternative ways in Python to return multiple values
  • 3. Keep learning

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 split_name(name):
    first, last = name.split()

    # Return two values at once
    return first, last

# Directly assign the values to two variables
first, last = split_name('John Wick')

# Use f-string to print both parts
print(f"{first=}, {last=}")Code language: Python (python)

Output:

first='John', last='Wick'

What we are actually doing here is returning a tuple. We could have written return (first, last) 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. It results in very readable, concise code because we can directly unpack them into clearly named variables.

However, once you get past 2 or 3 values, it gets hard to remember which value is which. At that point, give the values names. There are two good options for that in the standard library.

Return a NamedTuple

A NamedTuple is a tuple whose values also have names. It’s a small step up from a plain tuple: you can still unpack it, but you can also access the values by name:

from typing import NamedTuple


class Name(NamedTuple):
    first: str
    last: str


def split_name(name: str) -> Name:
    first, last = name.split()
    return Name(first, last)


result = split_name('John Wick')
print(result)
print(result.last)

# Unpacking still works, just like with a regular tuple
first, last = split_name('John Wick')
print(first)Code language: Python (python)

Output:

Name(first='John', last='Wick')
Wick
John

Like regular tuples, named tuples can’t be changed after you create them.

Return a data class

For more values, or when you want to change them later or add methods, use a data class. A data class is essentially a regular class annotated with @dataclass to give it some handy extra functionality, like a readable representation when you print it:

from dataclasses import dataclass


@dataclass
class Stats:
    minimum: float
    maximum: float
    average: float
    count: int


def describe(numbers: list[float]) -> Stats:
    return Stats(
        minimum=min(numbers),
        maximum=max(numbers),
        average=sum(numbers) / len(numbers),
        count=len(numbers),
    )


stats = describe([4, 8, 15, 16, 23, 42])
print(stats)
print(stats.average)Code language: Python (python)

Output:

Stats(minimum=4, maximum=42, average=18.0, count=6)
18.0

Unlike a tuple, a data class can’t be unpacked into separate variables directly. In return, code like stats.average tells the reader exactly what it’s using.

Return a dictionary, list, or class

You can also consider returning:

  • a Python dictionary,
  • a Python list,
  • or a regular class (object).

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, it will be cleaner to use a (data) class at some point.

Keep learning

  • Learn all about Python tuples and Python dictionaries
  • My introduction to functions
  • More advanced Python function tricks in our Python functions deep dive

Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

More about Erik

Try this yourself, right now

Return and unpack several values

The same topic as an interactive lesson: short explanation, then code you run and change in your browser, with instant feedback.

Open the interactive lesson

Free to start. Certificates and AI help with Guided.

Monty, your Python learning guide, smiling and waving

Leave a Comment Cancel reply

You must be logged in to post a comment.

Subscribe to my newsletter for Python news, tips, and tricks!

Footer subscribe

  • Home
  • Python Courses
  • Privacy Policy
  • About Us
  • Contact us
©2026 Python Land - All rights reserved
Python Land is not affiliated with Python.org or the Python Software Foundation
  • Free Tutorial
    • Install Python
      • Online Python Interpreter
      • How To Open Python on Windows, Mac, Linux
    • Introduction to Python
      • The Python REPL
      • Python Variable
      • Python String
      • Python Print function
      • Python Booleans
      • For-loop and While-loop
      • Python Functions
      • Your First Python Program
      • Python Comments
    • Creating Python Programs
      • The Best Python IDE
      • Installing VSCode
      • VSCode Python Extensions
      • VSCode GUI Tour
      • Python in VSCode: Running and Debugging
    • Classes and Objects in Python
      • Python Constructor
      • Python Inheritance
    • Structure Your Project
      • Python Modules And Importing
      • Python Packages
    • Python Data Types
      • Python Integer
      • Python Float
      • Python Tuple
      • Python List
      • Python Set
      • Python Dictionary
    • Language Deep Dives
      • Python Try Except (Exceptions)
      • Python Functions: Advanced Concepts
      • List Comprehension
      • Python Iterator
      • Python Range
      • Python Docstrings
      • Python pass (Do Nothing)
    • Interaction with the OS
      • Python Read And Write File
      • Python Subprocess: Run External Commands
    • Venvs / Package Management
      • Working With Python’s venv
      • Pip install: the Python package manager
      • Python Poetry: Package and venv Management Made Easy
      • Python Pipenv: Another Package Manager
      • Pipx: Safely Install Packages Globally
    • Python Concurrency
      • The Python GIL (Global Interpreter Lock)
      • Setting the Baseline
      • Python Threading
      • Python Multiprocessing
    • Data Processing with Python
      • Working With JSON
        • JMESPath Python: JSON Query Language
      • Python YAML: How to Load, Read, and Write YAML
      • Python CSV: Read And Write CSV Files
    • Migrating From Python 2 to 3
      • Check Python Version On The Command-Line
      • How to Migrate To Python 3
      • Python 3 Advantages
    • Using The Unix Shell
      • Basic Linux Commands
      • Bash Scripts
      • Using the Find Command in Linux
      • Unix Pipes
      • Process and Highlight JSON with jq
      • Using the Bash History
      • Bash Multiprocessing
    • Deployment
      • PyInstaller: Create An Executable From Python Code
      • How To Use Docker To Containerize Your Python Project
      • Automatically Build and Deploy Your Python Application with CI/CD
      • Guerrilla Scale Your Web Application
    • Data Science
      • Jupyter Notebook
      • NumPy: Getting Started Tutorial
    • Python Learning Resources
  • Python Courses
  • Blog
  • Login / Register