• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Return Multiple Values from Function in Python

Return Multiple Values from Function in Python

August 17, 2022 Leave a Comment

To return multiple values from a function in Python, you can use the return keyword and separate the values by commas. To access the returned values, use tuple unpacking.

def function_return_multiple():
    return 0, 1, 2, 3

a, b, c, d = function_return_multiple()

print(a)
print(b)
print(c)
print(d)

#Output:
0
1
2
3

You can also return multiple values as a list and then access the values just like you would access an item in a list.

def function_return_multiple():
    return 0, 1, 2, 3

a = function_return_multiple()

print(a)

#Output:
[0, 1, 2, 3]

When working with functions in Python, sometimes it can make sense where you want to return more than one value from a function.

To return multiple values from a function, you can use the concept of tuple unpacking.

Tuple unpacking allows you create a tuple and then create variables from the elements of the created tuple.

So, to return multiple values from a function in Python, you can use the return keyword and separate the values by commas. To access the returned values, use tuple unpacking.

Below shows you a simple example of how you can use tuple unpacking to return multiple values from a function in Python.

def function_return_multiple():
    return 0, 1, 2, 3

a, b, c, d = function_return_multiple()

print(a)
print(b)
print(c)
print(d)

#Output:
0
1
2
3

One thing to note here is that if you only care about the first few values, then if you don’t unpack all of the values, you will get a ValueError.

The ValueError occurs because you need to unpack all of the values returned.

If you don’t want to unpack all of the values, then you can use the unpacking operator *. This will return the remaining as a list.

Below shows you how to use the unpacking operator when returning multiple values from a function in Python.

def function_return_multiple():
    return 0, 1, 2, 3

a, *b = function_return_multiple()

print(a)
print(b)

#Output:
0
[1, 2, 3]

Return Multiple Values from Function as List in Python

Another way you can return multiple values from a function is by doing the same as above but instead of creating multiple variables, just create 1 variable.

Then to access the values, you can access them just like you would access an item in a list.

Below shows a simple example of how you can return multiple values as a list in Python.

def function_return_multiple():
    return 0, 1, 3,4 

a = function_return_multiple()

print(a)

#Output:
[0, 1, 2, 3]

Hopefully this article has been useful for you to understand how to return multiplevalues from a function in Python.

Other Articles You'll Also Like:

  • 1.  Get Elapsed Time in Seconds in Python
  • 2.  Python Indicator Function – Apply Indicator Function to List of Numbers
  • 3.  Python Decrement Counter with -= Decrement Operator
  • 4.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 5.  Run Something Every 5 Seconds in Python
  • 6.  Using Python to Increment Dictionary Value
  • 7.  Initialize Multiple Variables in Python
  • 8.  Pascal’s Triangle in Python
  • 9.  Count Number of Keys in Dictionary in Python
  • 10.  pandas median – Find Median of Series or Columns in DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy