How to return a tuple from a Python function? In Python, a tuple is a collection of ordered, immutable objects and you can return a tuple from a function by enclosing the objects you want to return in parentheses. In this article, I will explain how to return a tuple from a function in Python with some examples.
1. Quick Examples of Returning Tuple from Function
If you are in a hurry, below are some quick examples of the python return tuple from a function.
# Quick examples of python return tuple
# Example 1: Use the simplest python return tuple
def technology():
return ('Python', 25000)
tuples = technology()
# Example 2: Return tuple using arguments
def technology(courses, fees):
tempTuple = (courses, fees)
return tempTuple
tuples = technology('Python', 25000)
# Example 3: Return tuple using arguments
def my_function(x, y):
# some code here
return (x+y, x-y, x*y)
result = my_function(15, 8)
# Example 4: Returned tuple using unpack
def my_function(x, y):
return (x+y, x-y, x*y)
sum, diff, prod = my_function(15, 8)
# Example 5: Use generators
# Return multiple tuples from function
def technology():
tuples = [('Python', 25000), ('Spark', 30000), ('Pandas', 20000)]
for courses, fees in tuples:
yield courses, fees
for courses, fees in technology():
# Example 6: Return multiple tuples from function
def technology(courses, fees):
yield (courses[1], fees[0])
yield (courses[1], fees[1])
tuple1, tuple2 = technology([1, 2], ['Python', 25000])
1. Use the Simplest Python Return Tuple
You can use a tuple as the return value from a function in Python by simply enclosing the values you want to return in parentheses
– (), separated by commas. Returning a tuple from a function enables us to return multiple types of values from a function.
For example, let’s create a technology()
function and returns a tuple containing the values 'Python'
, 25000
. Call the function and assign the result to the tuples variable, and then print out the tuple to confirm that the function is returning a tuple correctly. The type of tuple is also confirmed to be a tuple using the type()
function.
# Use the simplest python return tuple
def technology():
return ('Python', 25000)
# Call function
tuples = technology()
# Print function returned values
print(tuples)
print(type(tuples))
# Output
# ('Python', 25000)
# <class 'tuple'>
2. Return Tuple Using Arguments
Similarly, let’s create another function technology()
that takes two arguments courses
and fees
and return these values as a tuple form the function.
# Return tuple using arguments
def technology(courses, fees):
tempTuple = (courses, fees)
return tempTuple
tuples = technology('Python', 25000)
print(tuples)
print(type(tuples))
# Output
# ('Python', 25000)
# <class 'tuple'>
You can also return a tuple from a function that takes expression. For example, the my_function(x, y)
takes two arguments x
and y
performs some computation on them and returns a tuple containing the sum
, difference
, and product
of x
and y
. The result variable is assigned to the returned tuple.
# Return tuple using arguments
def my_function(x, y):
# some code here
return (x+y, x-y, x*y)
result = my_function(15, 8)
print(result)
# Output
# (23, 7, 120)
You can also unpack the tuple into multiple variables. For example, the my_function(x, y)
returns a tuple containing the sum, difference, and product of x
and y
, and the returned tuple is unpacked into three separate variables sum
, diff
, and prod
.
# Returned tuple using unpack
def my_function(x, y):
return (x+y, x-y, x*y)
sum, diff, prod = my_function(15, 8)
print(sum)
print(diff)
print(prod)
# Output
# 23
# 7
# 120
3. Return Multiple Tuples from Function
You can use generators to return multiple tuples from a function.
# Use generators
# Return multiple tuples from function
def technology():
tuples = [('Python', 25000), ('Spark', 30000), ('Pandas', 20000)]
for courses, fees in tuples:
yield courses, fees
# Call the function and unpack the tuples
for courses, fees in technology():
print(courses, fees)
# Output
# Python 25000
# Spark 30000
# Pandas 20000
Here, the technology()
function creates a list tuples
containing courses
and fees
. It then uses a for
loop to iterate over the list and yield each tuple as it goes. When the function is called, it returns a generator object that can be used to iterate over the tuples
. In this case, you are using a for
loop to loop over the generator.
Follow the other example of returning multiple tuples from the function.
# Return multiple tuples from function
def technology(courses, fees):
yield (courses[1], fees[0])
yield (courses[1], fees[1])
tuple1, tuple2 = technology([1, 2], ['Python', 25000])
print(tuple1)
print(tuple2)
# Output
# (1, 'Python')
# (2, 25000)
Conclusion
In this article, I have explained how to return a python tuple from functions and also learned to return multiple tuples. Returning tuples from a function is a great feature where it allows to return multiple values of different types from a function
Happy Learning !!