• 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 / Using Python to Return Two Values from Function

Using Python to Return Two Values from Function

August 17, 2022 Leave a Comment

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

def function_return_two():
    return 0, 1

a, b = function_return_two()

print(a)
print(b)

#Output:
0
1

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

def function_return_two():
    return 0, 1

a = function_return_two()

print(a)

#Output:
[0, 1]

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 two or more 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 two values from a function in Python, you can use the return keyword and separate the values by commas. To access the two returned values, use tuple unpacking.

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

def function_return_two():
    return 0, 1

a, b = function_return_two()

print(a)
print(b)

#Output:
0
1

Return Two Values from Function as List in Python

Another way you can return two 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 two values as a list in Python.

def function_return_two():
    return 0, 1

a = function_return_two()

print(a)

#Output:
[0, 1]

Return Multiple Values from Function in Python

If you want to return multiple values from a function in Python, then you can use the method shown above and separate additional variables by commas.

Below shows an example of how you can return three values from a function in Python.

def function_return_three():
    return 0, 1, 2

a, b, c = function_return_three()

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

#Output:
0
1
2

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 more than two values from a function in Python.

def function_return_three():
    return 0, 1, 2

a, *b = function_return_three()

print(a)
print(b)

#Output:
0
[1, 2]

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

Other Articles You'll Also Like:

  • 1.  Get Week Number from Date in Python
  • 2.  Check if String Does Not Contain Substring in Python
  • 3.  How to Remove All Occurrences of a Character in a List Using Python
  • 4.  pandas mean – Get Average of Series or DataFrame Columns
  • 5.  How Clear a Set and Remove All Items in Python
  • 6.  Python Round to Nearest 10 With round() Function
  • 7.  Not Equal Operator != in Python
  • 8.  Using Python to Initialize Array of Size N
  • 9.  Add Key Value Pair to Dictionary in Python
  • 10.  Python max float – What’s the Maximum Float Value in Python?

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