Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7657955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:08:12+00:00 2026-05-31T13:08:12+00:00

I would like to return two values from a function in two separate variables.

  • 0

I would like to return two values from a function in two separate variables.
For example:

def select_choice():
    loop = 1
    row = 0
    while loop == 1:
        print('''Choose from the following options?:
                 1. Row 1
                 2. Row 2
                 3. Row 3''')

        row = int(input("Which row would you like to move the card from?: "))
        if row == 1:
            i = 2
            card = list_a[-1]
        elif row == 2:
            i = 1
            card = list_b[-1]
        elif row == 3:
            i = 0
            card = list_c[-1]
        return i
        return card

And I want to be able to use these values separately. When I tried to use return i, card, it returns a tuple and this is not what I want.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:08:13+00:00Added an answer on May 31, 2026 at 1:08 pm

    You cannot return two values, but you can return a tuple or a list and unpack it after the call:

    def select_choice():
        ...
        return i, card  # or [i, card]
    
    my_i, my_card = select_choice()
    

    On line return i, card i, card means creating a tuple. You can also use parenthesis like return (i, card), but tuples are created by comma, so parens are not mandatory. But you can use parens to make your code more readable or to split the tuple over multiple lines. The same applies to line my_i, my_card = select_choice().

    If you want to return more than two values, consider using a named tuple. It will allow the caller of the function to access fields of the returned value by name, which is more readable. You can still access items of the tuple by index. For example in Schema.loads method Marshmallow framework returns a UnmarshalResult which is a namedtuple. So you can do:

    data, errors = MySchema.loads(request.json())
    if errors:
        ...
    

    or

    result = MySchema.loads(request.json())
    if result.errors:
        ...
    else:
        # use `result.data`
    

    In other cases you may return a dict from your function:

    def select_choice():
        ...
        return {'i': i, 'card': card, 'other_field': other_field, ...}
    

    But you might want consider to return an instance of a utility class (or a Pydantic/dataclass model instance), which wraps your data:

    class ChoiceData():
        def __init__(self, i, card, other_field, ...):
            # you can put here some validation logic
            self.i = i
            self.card = card
            self.other_field = other_field
            ...
    
    def select_choice():
        ...
        return ChoiceData(i, card, other_field, ...)
    
    choice_data = select_choice()
    print(choice_data.i, choice_data.card)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to return a key value pair from a function. It would be
I would like to return a string with all of the contents of a
I would like to return the id of a newly created object for my
I would like to return an array of string in my web services I've
I have a method where I would like to return an object instance of
I have a script with a factory method that I would like to return
Given the following XML, I would like to return all eventtitles where the eventtype
Given the following directory: string fullpath = C:\MyDir1\MyDir2\MyDir3; I would like to return MyDir3
I'm constructing a regex that is looking for dates. I would like to return
I am using matplotlib in a django app and would like to directly return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.