DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

Return multiple values from a function in Python

In this guide, we look at Python: Return multiple values from a function. We explain the various methods in detail and list their shortcomings.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Content - Python return multiple values:

Python Return Multiple Values:

Functions are used to perform a specific task which most likely involves returning a particular value. However, a few use-cases may require you to return two or more values. In other programming languages, the code to achieve this might be quite complex but Python provides a few handy methods to return multiple values.

Most of these methods involve using a collection data type to return these values. We have taken a close look at all of them below.

Using Comma-separated values (Tuples):

In this method, we use Python to return multiple values by simply separating them by commas. Python basically uses a tuple to achieve this.

The code to achieve this is as follows:

#Returning Multiple Values using Tuples
def multiple():
    operation = "Sum" 
    total = 5+10
    return operation, total;

operation, total = multiple()

print(operation, total)
#Output = Sum 15
Enter fullscreen mode Exit fullscreen mode

A common confusion here is that the syntax of tuple requires a pair of brackets (). Although this is true, Python does not always require brackets to identify a tuple.

Using Python Lists:

Similar to the previous methods, lists can also be used to return multiple values, too. In case you are not familiar with lists, they are a collection of items. They are quite similar to arrays however they can be used to store values of various data types.

Since they are a collection of items we could add values to the list and return the list.

The code to return multiple values using list:

#Returning Multiple Values using List
def multiple():
    operation = "Sum" 
    total = 5+10
    return [operation, total];

values = multiple()

print(values)
#Output = ["Sum", 15]
Enter fullscreen mode Exit fullscreen mode

In this method, you would have to use the index of the list to use the values individually.

Using a Python Dictionary:

In the previous method, accessing a particular value would be a hassle. However, this would not be a problem while using dictionaries to return multiple values. This is because we could use relevant key names and they could be accessed easily.

The code to do so is as follows:

#Returning Multiple Values using Dictionary
def multiple():
    values = dict();
    values['operation'] = "Sum" 
    values['total'] = 5+10
    return values;

values = multiple()

print(values)
#Output = {'operation': 'Sum', 'total': 15}
Enter fullscreen mode Exit fullscreen mode

In this method, the returned values can easily be accessed within their keys.

Using a Python Data Class:

In this method, we use Data Classes to return multiple values. Data classes are a method of adding specific methods to a user-defined class. This method was introduced in Python version 3.7.

The code to do so is as follows:

#Returning Multiple Values using Data Classes
from dataclasses import dataclass

@dataclass
class multiplevalues():
    operation: str
    num1: int = 0
    num2: int = 0
    def values(self) -> float:
        return self.num1 + self.num2   

#passing arguments into the Data Class 
all_values = multiplevalues("Addition", 5, 10)
v = all_values.values() 

print(v)
print(all_values)
#Output
#15
#multiplevalues(operation='Addition', num1=5, num2=10)
Enter fullscreen mode Exit fullscreen mode

Limitations and Caveats - Python return multiple values

  • The first two methods, although used extensively, must be used with caution as they do not have the proper names to identify the values and hence can often lead to errors.
  • The only difference that list methods have over the tuple method is that lists are mutable.
  • The dictionary method is used when you are returning multiple values and find it hard to keep track of their names.
  • The Data Class methods can be a little complicated to understand at first, hence I would recommend practicing the other methods and later trying to understand this method.

Top comments (2)

Collapse
 
cyebukayire profile image
Peace

Thank you for the post:)

Collapse
 
hrishikesh1990 profile image
hrishikesh1990

You're Welcome. :)