Return multiple values from a function in Python
This post will discuss how to return multiple values from a function in Python.
1. Using Tuples
The preferred approach to return just two or three fields from a function in Python is using a Tuple. A tuple is a ordered and immutable collection of items, which are created by separating the values with commas and enclosing them within the parentheses. The idea is to pack values to be returned in a tuple, return the tuple from the function and unpack it inside the caller function. The following example shows how we can use tuples to return two fields of different types from a function.
|
1 2 3 4 5 6 7 8 |
def foo(): return 'A', 65 x, y = foo() print((x, type(x))) # ('A', <class 'str'>) print((y, type(y))) # (65, <class 'int'>) |
We can use tuples when the total values to be returned is less. The more the values, the more difficult it is to manage and correctly unpack them.
2. Using Named Tuples
To handle the problems associated with a tuple, we can use named tuples. A named tuple are tuple-like objects but have fields accessible by attribute lookup. We can create a named tuple by using the collections.namedtuple() built-in function in the collections module. Then, we can use indexing or dot notation to access each value in the named tuple by its position or name, respectively. The following code example shows how to implement named tuples:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import collections def foo(): Point = collections.namedtuple('Point', ['x', 'y', 'z']) return Point(x=1, y=(2, 3), z=[4, 5]) p = foo() print((p.x, type(p.x))) # (1, <class 'int'>) print((p.y, type(p.y))) # ((2, 3), <class 'tuple'>) print((p.z, type(p.z))) # ([4, 5], <class 'list'>) |
3. Using Dictionary
Instead of using a tuple or a named-tuple, we can also use a dictionary to return multiple values from a function in Python. This can be done by assigning each value to a key and enclosing them within curly braces. This can be used when the total number of values to be returned is more. For example:
|
1 2 3 4 5 6 7 8 9 |
def foo(): return {'x': 1, 'y': (2, 3), 'z': [4, 5]} d = foo() print((d['x'], type(d['x']))) # (1, <class 'int'>) print((d['y'], type(d['y']))) # ((2, 3), <class 'tuple'>) print((d['z'], type(d['z']))) # ([4, 5], <class 'list'>) |
4. Using Class
The last option to return multiple values from a function is by creating an instance of a class containing all fields and return it. The following code example implements a class used to return three fields of different types from the function foo().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class MyClass: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def foo(): return MyClass(x=1, y=(2, 3), z=[4, 5]) p = foo() print((p.x, type(p.x))) # (1, <class 'int'>) print((p.y, type(p.y))) # ((2, 3), <class 'tuple'>) print((p.z, type(p.z))) # ([4, 5], <class 'list'>) |
That’s all about returning multiple values from a function in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)