Blog Post #61: The Perfect Return: Using Tuples to Return Multiple Values from Functions

In Post #60, we learned about the art of sequence unpacking, a “Pythonic” way to assign items from a tuple or list to multiple variables at once. This elegant feature is more than just a convenient trick—it’s the key to one of the most powerful capabilities of Python functions.

In this post, we will explore a primary use case for tuples: returning multiple values from a single function.

The Function Rule: One Return to Rule Them All

While we haven’t formally covered creating our own functions in detail yet (that’s coming in Part 4), it’s helpful to know a fundamental rule: a function can only have one return statement that sends back a single value or object.

So what do we do if a function needs to produce multiple pieces of information? For example, a function that looks up a user and needs to return both their name and their email address.

The Solution: Pack Values into a Tuple

The answer is simple but powerful: you can’t return two things, but you can return one thing that contains two things. That perfect container for fixed data is a tuple.

A function can group multiple values into a tuple and return that single tuple object.

# A simple function that returns a user's name and role
def get_user_data():
    # In a real app, this might come from a database
    name = "Alice"
    role = "Admin"
    return (name, role) # We explicitly return one tuple containing two values

Unpacking the Return Value

When we call this function, we get a single tuple back. We can then use the unpacking technique we just learned to immediately assign the contents of that tuple to separate, meaningful variables.

# Call the function and unpack the returned tuple into two variables
user_name, user_role = get_user_data()

print(f"Welcome, {user_name}! Your role is: {user_role}.")

The output will be: Welcome, Alice! Your role is: Admin.

This is the standard and most readable way to handle multiple return values. It’s far cleaner than storing the tuple in one variable and then accessing the items with indexes like user_data[0] and user_data[1].

Implicit Tuples and a Practical Example

Python makes this pattern even more elegant. It’s smart enough to know that when you return multiple values separated by commas, you intend to create a tuple. You don’t even need to type the parentheses.

def find_min_and_max(scores):
    # For now, we can use Python's built-in min() and max() functions
    lowest = min(scores)
    highest = max(scores)
    
    # Python automatically creates and returns a tuple: (lowest, highest)
    return lowest, highest

grades = [88, 99, 54, 78, 92]

# Unpack the returned tuple directly
low_grade, high_grade = find_min_and_max(grades)

print(f"The lowest grade was: {low_grade}")
print(f"The highest grade was: {high_grade}")

The output will be:

The lowest grade was: 54
The highest grade was: 99

What’s Next?

The combination of Python’s ability to automatically pack return values into a tuple and our ability to unpack them into variables is a powerful and expressive pattern. It’s the standard, “Pythonic” way to get multiple pieces of information back from a function call.

We’ve now seen what lists and tuples are and explored their key features and differences, from mutability to unpacking. But the question remains: when should you choose one over the other? In Post #62, we will provide a clear, practical guide on when to use a list versus when to use a tuple.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment