Returning Multiple Values in Python

25 Nov, 2020
In Python, we can return multiple values from a function. Following are different ways
1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class.
# A Python program to return multiple # values from a method using classclass Test:    def __init__(self):        self.str = "geeksforgeeks"        self.x = 20     # This function returns an object of Testdef fun():    return Test()      # Driver code to test above methodt = fun() print(t.str)print(t.x)
Output:
geeksforgeeks
20
Below are interesting methods for somebody shifting C++/Java world.
 
2) Using Tuple: A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple and list.
# A Python program to return multiple # values from a method using tuple  # This function returns a tupledef fun():    str = "geeksforgeeks"    x   = 20    return str, x;  # Return tuple, we could also                    # write (str, x)  # Driver code to test above methodstr, x = fun() # Assign returned tupleprint(str)print(x)
Output:
geeksforgeeks
20
 
3) Using a list: A list is like an array of items created using square brackets. They are different from arrays as they can contain items of different types. Lists are different from tuples as they are mutable.
# A Python program to return multiple # values from a method using list  # This function returns a listdef fun():    str = "geeksforgeeks"    x = 20       return [str, x];    # Driver code to test above methodlist = fun() print(list)
Output:
['geeksforgeeks', 20]
 
4) Using a Dictionary: A Dictionary is similar to hash or map in other languages. See this for details of dictionary.
# A Python program to return multiple # values from a method using dictionary  # This function returns a dictionarydef fun():    d = dict();     d['str'] = "GeeksforGeeks"    d['x']   = 20    return d  # Driver code to test above methodd = fun() print(d)
Output:
{'x': 20, 'str': 'GeeksforGeeks'}
 5) Using Data Class (Python 3.7+): In Python 3.7 and above the Data Class can be used to return a class with automatically added unique methods. The Data Class module has a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() in the user-defined classes.
from dataclasses import dataclass  @dataclassclass Book_list:    name: str    perunit_cost: float    quantity_available: int = 0              # function to calculate total cost        def total_cost(self) -> float:        return self.perunit_cost * self.quantity_available      book = Book_list("Introduction to programming.", 300, 3)x = book.total_cost()  # print the total cost# of the bookprint(x)  # print book detailsprint(book)  # 900Book_list(name='Python programming.',          perunit_cost=200,          quantity_available=3)
Output:
900
Book_list(name='Introduction to programming.', perunit_cost=300, quantity_available=3)
Book_list(name='Python programming.', perunit_cost=200, quantity_available=3)
Reference:http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python
This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
nidhi_biet
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Python map() function
Adding new column to existing DataFrame in Pandas
Python Dictionary
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Taking input in Python
Enumerate() in Python
Read a file line by line in Python
Python String | replace()