Python offers several different ways of returning more than one variable from a function. Which one to be used depend on many factors. In this post are covered some of them and when they are appropriate for use:

  • named tuple named tuple - possibly the optimal way to return multiple values for most cases
  • tuple tuple - it's OK for small projects
  • dictionary dictionary - you can add new values without breaking the program logic
  • list - good for long not changing values

Python Return Multiple Variables as a Named Tuple

Using namedtuple for returning mutliple values from a function is one of the recommended options. It has advantage to the normal tuple because allows you to access output values in two ways:

print(calc(a , b).sumval)
print(calc(a , b)[0])

Full example:

from collections import namedtuple
def calc(x, y):
    sumval = x + y
    absval = abs(y - y)
    percx = (100 * float(x)/float(y))
    percy = (100 * float(y)/float(x))
    result = namedtuple('values', ['sumval', 'absval', 'percx', 'percy'])
    return result(sumval, absval, percx, percy)

a = 4
b = 15
print(calc(a , b))
print(calc(a , b).sumval)
print(calc(a , b)[0])
sumval, absval, percx, percy = calc(a , b)
print (sumval)

result:

values(sumval=19, absval=0, percx=26.666666666666668, percy=375.0)
19
19
19

This way is preferred by many and there's no risk of getting the wrong values. Another advantage is that you can get all values at once. The only concern about using named tuples is about performance for longer tuples.

Python Return Multiple Variables as a Tuple(normal)

This could be one of the most used ways to return multiple variables from Python. It's simple and can be described as pythonic way of code:

print(calc(a , b)[0])

Full example:

def calc(x, y):
    sumval = x + y
    absval = abs(y - y)
    percx = (100 * float(x)/float(y))
    percy = (100 * float(y)/float(x))
    return (sumval, absval, percx, percy)

a = 4
b = 15
print(calc(a , b))
print(calc(a , b)[0])

result:

(19, 0, 26.666666666666668, 375.0)
19

Note that if you try to get the values by name like:

print(calc(a , b).sumval)

you will end with error:

AttributeError: 'tuple' object has no attribute 'sumval'

Python Return Multiple Variables as a Dict

Returning variables as a dictionary in Python is preferred way for some developers. You have relatively good way to access output values by:

print(calc(a , b)['sumval'])

Full example:

def calc(x, y):
    sumval = x + y
    absval = abs(y - y)
    percx = (100 * float(x)/float(y))
    percy = (100 * float(y)/float(x))
    return {'sumval':sumval, 'absval':absval, 'percx':percx, 'percy':percy}

a = 4
b = 15
print(calc(a , b))
print(calc(a , b)['sumval'])

{'percx': 26.666666666666668, 'percy': 375.0, 'sumval': 19, 'absval': 0}
19

The obvious advantage of using dictionaries for return values is that you can add new values without breaking the code logic.

Note: that for dictionary you can get wrong values if you try to do:

sumval, absval, percx, percy = calc(a , b)
print (sumval)

which results in:

percx

Python Return Multiple Variables as a List

The option to return multiple values as list will be intuitive for people coming from some other languages. This is not typical for python and could be a bit misleading. If the number of the output values is changed this can break the code. The access to the variables should be done by indexes:

print(calc(a , b)[0])

Full example:

# list
def calc(x, y):
    result = []
    result.append(x + y)
    result.append(abs(y - y))
    result.append((100 * float(x)/float(y)))
    result.append((100 * float(y)/float(x)))
    return result

a = 4
b = 15
print(calc(a , b))
print(calc(a , b)[0])

[19, 0, 26.666666666666668, 375.0]
19
19

This one could be good if you have constant big list of return values.