What Are The Alternatives for Returning Multiple Values From A Python Function

In this post we will learn the alternatives for returning multiple values from a Python function as there are several ways to do so which have listed below using  tuple, dictionary, Using mutable objects, and using Global variables even by using a named tuple we can do the same and here we have discussed everything related to it below


Returning Multiple Values

Here we have given an example where we have to return multiple values in a program and we have tackled the same problems in different ways for different programs

Using A Tuple

Here we have taken a tuple for multiple value in a program and to learn more about the same just follow the example given below and get things done.

def my_ function():
    return 1, 2, 3

a, b, c = my_function()

Here we have a function named as my function and some values to print so we used a tuple.

Using Dictionary

For the example given above we can also use a dictionary for implementing the same and for the same purpose we have given an example below to get it done as per the nomes provided.

def my_function():
    return {'a': 1, 'b': 2, 'c': 3}

result = my_function()

This example will also print the same output as the [previous one did but the only difference here is we have used a dictionary here instead of using a tuple before.

Using Mutable Object

As we know we can also use mutable objects too for printing multiple values in a program and for the same we have given an example below.

def my_function( my_list):

result = []
my_function( result)

The above example will also result in the same as the previous two as our primary motive is to print the multiple values as we had done before.

Using Global Variables

We can also use a global variable for the same purpose as we keep on modifying the variable value inside a loop and keep the updated values printed every time we want. This way we can avoid using multiple variables or any long variables to store the value rather we keep on updating the older value itself and keep on printing the same when we required.





To learn more about Alternatives for returning multiple values from a Python function visit:  by stack overflow

To learn more about solutions to different problems and tutorials for the concepts we need to know to work on programming along with different ways to solve any generally asked problems: How To Pass-Variables From A Java & Python Client To A Linux/Ubuntu Server Which Is Running C?.

Leave a Comment

%d bloggers like this: