2

Return multiple values from a function in Python

 1 year ago
source link: https://thispointer.com/return-multiple-values-from-a-function-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this Python tutorial, we will discuss how to return multiple values from a function in Python.

Table Of Contents

Let’s Dive into the tutorial.

Return multiple values from a function in Python using Tuple

To return value we have to specify it with the return keyword. When we specify more than one value in the return statement, then automatically, those values are returned in the form a tuple.

Syntax:

Advertisements

def function():
return value1,value2, value3, value4
print(function())
def function():
    return value1,value2, value3, value4

print(function())

Where a function() is the name of the function. It will return a tuple comtaining multiple returned values.

Example:

In this example, we will create a function named – my_string() and return three string values.

# This function returns 3 values
def my_string():
return "welcome","to","thisPointer"
# call the function - my_string()
values = my_string()
print(values)
# Access Individual returned values
value1 = my_string()[0]
value2 = my_string()[1]
value3 = my_string()[2]
print(value1)
print(value2)
print(value3)
# This function returns 3 values
def my_string():
    return "welcome","to","thisPointer"

# call the function - my_string()
values = my_string()

print(values)

# Access Individual returned values
value1 = my_string()[0]
value2 = my_string()[1]
value3 = my_string()[2]

print(value1)
print(value2)
print(value3)

Output:

('welcome', 'to', 'thisPointer')
welcome
thisPointer
('welcome', 'to', 'thisPointer')
welcome
to
thisPointer

We can see that values are returned in a tuple.

Return multiple values from a function in Python using a Comma

When we specify more than one value in the return statement, then those values will be returned as a tuple. Now, we can separate values inside the tuple using a comma and assign them to new variables.

Syntax:

def getValues():
value1 = 10
value2 = 11
value3 = 12
return value1, value2, value3
value1, value2, value3 = getValues()
print(value1, value2, value3)
def getValues():
    value1 = 10
    value2 = 11
    value3 = 12
    return value1, value2, value3

value1, value2, value3 = getValues()

print(value1, value2, value3)

Output:

10 11 12

Where value1, value3, value3 are the new variables in which tuple values are assigned. Here, where the getValues() is the name of the function.

Example:

In this example, we will create a function named – my_string() and return three string values separated by a comma.

def my_string():
return "welcome", "to", "thisPointer"
# call the function - my_string() and assign values
# present inside tuple to variables
# using comma operator.
value1, value2, value3 = my_string()
# Display the variables
print(value1)
print(value2)
print(value3)
def my_string():
    return "welcome", "to", "thisPointer"

# call the function - my_string() and assign values
# present inside tuple to variables
# using comma operator.
value1, value2, value3 = my_string()

# Display the variables
print(value1)
print(value2)
print(value3)

Output:

welcome
thisPointer
welcome
to
thisPointer

We can see that values are not returned in a tuple. They got assigned to different variables.

Return multiple values from a function in Python using List

To return more than one value using return statement, we can add them in a list and return the list. We can use [] opeartor to create a list of all values that need to be returned and then provide that list object to the return keyword.

Syntax:

def function():
return [value1, value2, value3, value4]
print(function())
def function():
    return [value1, value2, value3, value4]

print(function())

Where a function() is the name of the function. It will return a list object, which contains multiple values.

Example:

In this example, we will create a function named – my_string() and return three string values in a list.

# Define a function - my_string() that
# returns 3 values in a list
def my_string():
return ["welcome", "to", "thisPointer"]
# call the function - my_string()
values = my_string()
print(values)
# Access individual values from list
print( values[0] )
print( values[1] )
print( values[2] )
# Define a function - my_string() that
# returns 3 values in a list
def my_string():
    return ["welcome", "to", "thisPointer"]

# call the function - my_string() 
values = my_string() 

print(values)

# Access individual values from list
print( values[0] )
print( values[1] )
print( values[2] )

Output:

['welcome', 'to', 'thisPointer']
welcome
thisPointer
['welcome', 'to', 'thisPointer']
welcome
to
thisPointer

We can see that values are returned in a list.

Otherwise, you can also use the list() function while calling a function. In that case, you no need to place values inside [] in the return statement.

Syntax:

def function():
return value1,value2,...........
print(list(function()))
def function():
    return value1,value2,...........

print(list(function()))

Where the function() is the name of the function. Instead of creating a list object inside the function, we returned multiple values directly and then created a list object with the returned values.

Example:

In this example, we will create a function named – my_string() and return three string values in a list.

# Define a function - my_string() that
# returns 3 values in a list
def my_string():
return "welcome", "to", "thisPointer"
# call the function - my_string()
values = list(my_string())
print(values)
# Access individual values from list
print( values[0] )
print( values[1] )
print( values[2] )
# Define a function - my_string() that
# returns 3 values in a list
def my_string():
    return "welcome", "to", "thisPointer"

# call the function - my_string() 
values = list(my_string()) 

print(values)

# Access individual values from list
print( values[0] )
print( values[1] )
print( values[2] )

Output:

['welcome', 'to', 'thisPointer']
welcome
thisPointer
['welcome', 'to', 'thisPointer']
welcome
to
thisPointer

We can see that values are returned in a list.

Return multiple values from a function in Python using Dictionary

We can use the dictionary of values in a return statement.

Returned Format:

return {key1: value1, key2: value2, ...........}
return {key1: value1, key2: value2, ...........}

Syntax:

def function():
return {key1: value1, key2: value2, ...........}
print(function())
def function():
    return {key1: value1, key2: value2, ...........}

print(function())

where the function is the name of the function.

Example:

In this example, we will create a function named – my_string() and return three items in a dictionary.

# Define a function - my_string() that
# returns 3 values inside a dictionary
def my_string():
return {1: "welcome",
2: "to",
3: "thisPointer"}
# Call the function - my_string()
print(my_string())
# Define a function - my_string() that
# returns 3 values inside  a dictionary
def my_string():
    return {1:  "welcome",
            2:  "to",
            3:  "thisPointer"}

# Call the function - my_string()
print(my_string())

Output:

{1: 'welcome', 2: 'to', 3: 'thisPointer'}
{1: 'welcome', 2: 'to', 3: 'thisPointer'}

We can see that values are returned in a dictionary.

Return multiple values from a function in Python using Set

When we specify more than one value in the return statement, then automatically, those values are returned in the tuple. If we want to return in the form of a set, then we can use set() while calling the function. If there are any duplicate values in the return statement, then the set will not allow it. It will return only the unique elements.

Example 1:

In this example, we will create a function named – my_string() and return three string values in a set.

# Define a function that returns 3 values
def my_string():
return "welcome", "to", "thisPointer"
# Call the function - my_string() and
# get rerurned values in the form of a set
print( set(my_string()) )
# Define a function that returns 3 values
def my_string():
    return "welcome", "to", "thisPointer"

# Call the function - my_string() and
# get rerurned values in the form of a set
print( set(my_string()) )

Output:

{'thisPointer', 'welcome', 'to'}
{'thisPointer', 'welcome', 'to'}

We can see that values are returned in a set.

Example 2:

In this example, we will create a function named – my_string() and return six-string values in a set. THe returned values will have duplicates, which will be filtered in a set, because a set contains only unique values.

# Define a function that returns 6 values
# with some duplicate values
def my_string():
return "welcome", "to", "thisPointer", "welcome", "to", "thisPointer"
# Call the function - my_string() and
# get rerurned values in the form of a set
print( set(my_string()) )
# Define a function that returns 6 values
# with some duplicate values
def my_string():
    return "welcome", "to", "thisPointer", "welcome", "to", "thisPointer"

# Call the function - my_string() and
# get rerurned values in the form of a set
print( set(my_string()) )

Output:

{'to', 'welcome', 'thisPointer'}
{'to', 'welcome', 'thisPointer'}

We can see that values are returned in a set without duplicates.

Summary

From this tutorial, we have seen that it can be possible to return multiple values from a function using a tuple, list, set and dictionary. Based on the requirements, you can use any of the above-used data structures. If you don’t want to allow duplicates, then set() is preferred.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK