Python returning multiple values – Python Program to Return Multiple Values From a Function

Functions in Python:

Python returning multiple values: A function in Python is a set of related statements that performs a single task.

Functions aid in the division of our program into smaller, modular chunks. Functions help our program  become more structured and manageable as it increases in size.

It also eliminates duplication and makes the code reusable.

Given a function, the task is to return multiple values from the function.

Return Multiple Values From a Function in Python

There are several ways to return multiple values from a function some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Returning as Tuple

Tuple:

A tuple is an immutable, ordered set. A tuple cannot shift in this way.

For example, a tuple can be used to store information about a person’s name, age, and location.

You can return multiple values in Python by simply separating them with commas.

As an example, write the following function that returns a string and a number: Simply write each value, separated by commas, after the return.
Except where syntax requires parentheses, comma-separated values are treated as tuples in Python.
As a result, in the below example, the function returns a tuple with each value as an element.
It’s worth noting that it’s the comma, not the parentheses, that creates a tuple. Except in the case of an empty tuple or where necessary to prevent syntactic ambiguity, parentheses are optional.

Below is the implementation:

# writing a sample function which returns the multiple values
def sampleFunc():
    return 11, "Hello", 5.5, True, 353

# Driver code
print(sampleFunc())

Output:

(11, 'Hello', 5.5, True, 353)

Explanation:

Here this sampleFunc returns different datatypes and multiple values.

Writing the code to print type of values it returned:

# writing a sample function which returns the multiple values
def sampleFunc():
    return 11, "Hello", 5.5, True, 353


# Driver code
print(type(sampleFunc()))
print(type(sampleFunc()[0]))
print(type(sampleFunc()[1]))
print(type(sampleFunc()[2]))
print(type(sampleFunc()[3]))

Output:

<class 'tuple'>
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>

Limitations:

However, as the number of values returned grows, the above programme becomes problematic. If we want to return six or seven values, what do we do? We can keep tupling them, but it’s easy to lose track of which meaning belongs where. Also, unpacking them anywhere we want to obtain them can be a little messy.

Unpacking the tuple:

# writing a sample function which returns the multiple values
def sampleFunc():
    return 11, "Hello", 5.5, True, 353

# unpacking the tuple
print(*sampleFunc())

Output:

11 Hello 5.5 True 353

Method #2:Returning as List

List:

A list is a mutable, ordered sequence. That is to say, a list can be modified.

We can return multiple values from the function using [] .

It is quick to confuse tuples and lists. After all, they are both containers that hold items. However, keep the following differences in mind:

  • Tuples cannot be modified.
  • Lists are subject to change.

Below is the implementation:

# writing a sample function which returns the multiple values
def sampleFunc():
    return [11, "Hello", 5.5, True, 353]


# print the return values
print(sampleFunc())

Writing the code to print type of values it returned:

# writing a sample function which returns the multiple values
def sampleFunc():
    return [11, "Hello", 5.5, True, 353]


# Driver code
print(type(sampleFunc()))
print(type(sampleFunc()[0]))
print(type(sampleFunc()[1]))
print(type(sampleFunc()[2]))
print(type(sampleFunc()[3]))

Output:

<class 'list'>
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>

Unpacking the list:

# writing a sample function which returns the multiple values
def sampleFunc():
    return [11, "Hello", 5.5, True, 353]

# unpacking the tuple
print(*sampleFunc())

Output:

11 Hello 5.5 True 353

Method #3: Returning as Dictionary

When you use a dictionary to return values, it’s simple to keep track of the returned values by using the keys.

Here we return multiple values as dictionary.

Below is the implementation:

# writing a sample function which returns the multiple values
def sampleFunc():
    return {1: 11, 2: "Hello", 3: 5.5, 4: True, 5: 353}


# Driver code
print(sampleFunc())

Output:

{1: 11, 2: 'Hello', 3: 5.5, 4: True, 5: 353}

Here it returns multiple values as dictionary.
Related Programs: