How to return multiple values in python?

The return statement exits the function and returns a value back to the caller. A return statement can return multiple values to the caller. This article discusses several ways to return multiple values from a function.

Returning multiple values using tuples

A tuple is a group of elements separated by commas, enclosed within () brackets. Multiple values can be returned using a tuple.

For example, my_function() returns two values ("Welcome To Python", 3) . The values in the tuples get unpacked and are assigned to val_1, val_2.

def my_function():
    return ("Welcome To Python" , 3)

val_1 , val_2 = my_function()
print(val_1, val_2)

The above program gives output as

Welcome To Python 3

Returning multiple values using List

A list is a group of elements separated by commas, enclosed within [] brackets. Multiple values can be returned using a list.

For example, my_function() returns two values ["Welcome To Python", 3] . The values in the list get unpacked and are assigned to val_1, val_2.

def my_function():
    return ["Welcome To ", "Chercher Tech"]

val_1 , val_2 = my_function()
print(val_1, val_2)

The above program gives output as

Welcome To  Chercher Tech

Returning multiple values using a dictionary

A dictionary is a built-in data type in python that stores the data as key-value pair. Multiple values can be returned using a dictionary. The below example illustrates the use of a dictionary to return multiple values.

def my_function():
    return {"val_1": "Welcome To ", "val_2": "Chercher Tech"}

d = my_function()
print(d)

Output

{'val_1': 'Welcome To ', 'val_2': 'Chercher Tech'}

Returning multiple values using an object

A class is created to hold multiple values and the object of the class is returned by a function to return multiple values.

For example, consider a class my_class. def __init__(self) in my_class is a constructor with two values val_1 initialized to "Welcome to Python" and val_2 initialized to 3. obj is an object of my_class. When the function my_function() is called the obj is returned as a value.

class my_class:
    def __init__(self):
        self.val_1 = "Welcome To Python"
        self.val_2 = 3
def my_function():
     obj = my_class()
     return obj
my_obj = my_function()
print(my_obj.val_1, my_obj.val_2)

Output

Welcome To Python 3
When we have fewer variables to accept the values returned by a function ValueError exception occurs.

For example, Consider a function my_function() that returns three values ["Welcome To", "Chercher", "Tech"]. But have only two variables val_1 and val_2 to accept the returned values. In such cases, ValueError exception occurs.

def my_function():
    return ["Welcome To ", "Chercher", "Tech"]

val_1 , val_2 = my_function()
print(val_1, val_2)

The output of the above program results in a ValueError exception.

val_1 , val_2 = my_function()
ValueError: too many values to unpack (expected 2)
Similarly, when we have more variables to accept the values returned by function ValueError exception occurs.

For example, Consider a function my_function() that returns three values ["Welcome To", "Chercher", "Tech"]. But have four variables val_1, val_2, val_3, val_4 to accept the returned values. In such cases, ValueError exception occurs.

def my_function():
    return ["Welcome To ", "Chercher", "Tech"]

val_1 , val_2, val_3, val_4 = my_function()
print(val_1, val_2)

The output of the above program results in a ValueError exception.

val_1 , val_2, val_3, val_4 = my_function()
ValueError: not enough values to unpack (expected 4, got 3)

To resolve the issue, when we don't know the number of values returned by a function we can use the * operator followed by the variable name.

The below example illustrates the use of the * operator. The val_1 is assigned to "Welcome To" and the remaining returned values are assigned to val_2.

def my_function():
    return ["Welcome To ", "Chercher", "Tech"]

val_1 , *val_2 = my_function()
print(val_1, val_2)

The above code returns the output as

Welcome To  ['Chercher', 'Tech']
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions