python return multiple values

How to Return Multiple Values from Function in Python

Sometimes you may need to return multiple values from function in python. In this article, we will look at the different ways to do this. Python provides several data structures such as tuples, lists & dictionaries that can store multiple values. Python allows you to return these data types from function, thereby allowing you to return multiple values.


How to Return Multiple Values from Function in Python

There are different ways to return multiple values from function in python.


1. Using lists

Any python data structure that can hold multiple values, such as list, tuples and dictionaries can be returned from python function. Here is an example to return a list that contains multiple values, from python. Please note, since a list can contain data of different types, you can use it to return different types of data from function.

def h(x):
  result = [x + 1]
  result.append(x + 2)
  result.append('abc')
  return result


2. Using tuples

Similarly, you can also use tuples to return multiple values from python function. But tuples are immutable, unlike lists.

def f(x):
  y0 = x + 1
  y1 = x * 2
  y2 = 'abc'
  return (y0, y1, y2)


3. Using dictionary

Dictionary is a collection of key-value pairs that is mutable. You can also return a dictionary from python function as shown in the following example.

def g(x):
  y0 = x + 1
  y1 = x * 2
  y2 = y0 ** 3
  return {'y0': y0, 'y1': y1 ,'y2': y2}


4. Using classes

If your application or website uses classes, then you can also use an object to return multiple values in python. Here is an example of class ReturnValue that contains 3 member variables. When you return an object of this class, it automatically contains the 3 variables created during the object’s assignment.

class ReturnValue:
  def __init__(self, y0, y1, y2):
     self.y0 = y0
     self.y1 = y1
     self.y2 = y2

def g(x):
  y0 = x + 1
  y1 = x * 2
  y2 = y0 ** 3
  return ReturnValue(y0, y1, y2)


5. Using dataclass

Since python 3.7, you can also use dataclasses that provide special methods for variables. Here is an example where we define ReturnValue data class with 3 variables y0, y1, and y2 along with their data types. You will notice that their values are automatically assigned in total_cost function, even though we have not defined them because these methods are already available for each variable in a dataclass.

@dataclass
class Returnvalue:
    y0: int
    y1: float
    y2: int

def total_cost(x):
    y0 = x + 1
    y1 = x * 2
    y2 = y0 ** 3
    return ReturnValue(y0, y1, y2)

Please note, dataclass is available only since python 3.7.

In this article, we have learnt several ways to return multiple values from python function. You can use any of them as per your requirement.

Also read:

How to Iterate Through List of Dictionaries in Python
How to Setup vnstat Network Monitoring Tool in Linux
How to Configure NFS Share in Ubuntu
How to Download Directory & Subdirectory using Wget
How to Backup & Restore Odoo Database

Leave a Reply

Your email address will not be published. Required fields are marked *