Concepts / Working with Multiple Data Types

Working with Multiple Data Types

A function can return multiple values by packaging them into a single tuple object using the comma syntax in the return statement.

  • Programming

One Result with Several Parts

Many functions need to communicate more than one piece of information to the code that called them. A validation function might need to provide both a success status and an error message. A data-processing function might need to provide both a result and information about the operation. Python handles this by packaging the values into one tuple object.

Key point

A function that appears to return several values is returning one tuple containing those values. The tuple gives the caller one returned object whose elements can be accessed together or distributed into separate variables.

check_input()functionsuccesstuple elementerror messagetuple elementreturned tupleone objectreturnscontainscontains
What does a function return contain when values are separated by commas in the return statement?

The Return Tuple

The syntax for returning multiple values uses a return statement followed by the values separated by commas. The values are packaged into a tuple. Parentheses may be used around the values, but the comma is the important part that creates the tuple. This means the returned result is one tuple object, not several independent return channels.

python
Output
The function returns one tuple containing two elements: the status value 404 and the message "Page not found".
returned tuple404, Page not foundresult[0]404errnum404errstrPage not foundindexunpackunpack
What is the difference between keeping the returned tuple together and assigning its elements to separate variables?

Unpacking in Order

Tuple unpacking distributes the elements of one returned tuple into multiple variables in a single assignment statement. Python first executes the function and receives its tuple. It then compares the number of variables on the left side with the number of tuple elements. Finally, it assigns each element to the corresponding variable in order.

Code walkthrough
def get_error_details():
    return 404, "Page not found"

errnum, errstr = get_error_details()
  1. 1
    The function executes and returns a tuple with two elements.
  2. 2
    The variables errnum and errstr provide two positions for the two returned elements.
  3. 3
    The first tuple element, 404, is assigned to errnum.
  4. 4
    The second tuple element, Page not found, is assigned to errstr.
404tuple position 0errnumreceives 404Page not foundtuple position 1errstrreceives Page not foundposition 0position 1
How does each value in a returned tuple move into its corresponding variable during unpacking?
Worked example

Reading a Validation Result

A validation function returns a success status and an error message. How can the caller work with both results separately?

  1. 1
    Package

    The function returns the status and message as elements of one tuple.

  2. 2
    Match

    The caller writes two variables because the returned tuple contains two elements.

  3. 3
    Assign

    The first returned element goes to success, and the second goes to message.

Result

The caller can use success and message as separate variables after one tuple-unpacking assignment.

Indexing or Unpacking

ApproachWhat it doesUseful when
IndexingKeeps the returned tuple as one object and accesses elements with bracket notationYou want the values together or need only some values
UnpackingDistributes tuple elements into separate variables in one assignmentYou know how many values are returned and want to use them individually

Both approaches access values from a returned tuple, but they emphasize different ways of working with those values.

python

The first part of this example keeps the tuple in details and uses indexing to retrieve its elements. The second assignment immediately unpacks the returned tuple. Unpacking is generally preferred when the number of returned values is known and the values should be used as individual variables. Indexing remains useful when the tuple should stay together or only selected elements are needed.

Mistakes with Tuple Returns

  • Common mistake

    Treating a tuple return as several independent return channels

    What it looks like

    Assuming the function sends one value to one place and another value to a separate place

    Why it fails

    The function packages the values into one tuple object before the caller receives them.

    Do this instead

    Receive the tuple as one object, or unpack its elements into variables.

  • Common mistake

    Using the wrong number of variables during unpacking

    What it looks like

    Writing three variables when the returned tuple contains two elements

    Why it fails

    Tuple unpacking requires the variables on the left side to match the number of returned tuple elements.

    Do this instead

    Use a matching number of variables, or keep the tuple together and access the needed elements by index.

  • Common mistake

    Confusing indexing with unpacking

    What it looks like

    Using bracket notation when the goal is to give each returned value a descriptive variable name

    Why it fails

    Indexing accesses elements through positions, while unpacking assigns elements to variables in order.

    Do this instead

    Use unpacking when the known returned values should be handled as separate variables.

  • Common mistake

    Forgetting that order controls assignment

    What it looks like

    Writing the variables in an order that does not correspond to the tuple elements

    Why it fails

    Python assigns each element to the variable in the same position.

    Do this instead

    Place the variables in the same order as the values returned by the function.

two tuple values404, Page not foundthree variablesmismatched counttwo tuple values404, Page not foundtwo variablesmatching countcannot matchunpacks in order
What happens when the number of variables used for unpacking does not match the number of values in the returned tuple?

Practice the Choice

EASY

A function returns a tuple containing a processing result and metadata about how long the operation took. Describe which approach you would choose in each situation: keep both pieces together for later use, access only the first piece, or work with both pieces through descriptive variable names.

Hints
  • Keeping the returned values together suggests treating the tuple as one object.
  • Accessing only one element can be done with bracket notation.
  • Using both values as individual variables is the main use case for unpacking.

What do you think happens?

A function returns two tuple elements. If the caller writes two variables on the left side of the assignment, how are the elements assigned?

  • Both elements are assigned to the first variable

  • The first element goes to the first variable and the second goes to the second variable

  • The tuple remains inaccessible

  • The second element is discarded

Reveal answer
Answer:

The first element goes to the first variable and the second goes to the second variable.

Tuple unpacking matches the number of variables with the number of tuple elements and assigns corresponding elements in order.

The Complete Pattern

  1. 1
    A function can communicate multiple values by packaging them into one tuple.
  2. 2
    The comma in the return statement is what matters for creating the tuple; parentheses are optional.
  3. 3
    Tuple unpacking distributes returned elements into separate variables in order.
  4. 4
    Use unpacking when the number of returned values is known and the values should be handled individually.
  5. 5
    Use indexing when you want to keep the tuple together or need only some of its elements.

Key Takeaways

  • Functions return one tuple object when multiple values are separated by commas in a return statement.
  • Tuple unpacking assigns tuple elements to separate variables in corresponding order.
  • The number of variables used for unpacking must match the number of returned tuple elements.
  • Indexing and unpacking are both valid, but unpacking is preferred when known values should be used individually.
  • Indexing is useful when values should remain together or only selected tuple elements are needed.

Related concepts

More concepts starting with W