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.
You cannot return two values, but you can return a
tupleor alistand unpack it after the call:On line
return i, cardi, cardmeans creating a tuple. You can also use parenthesis likereturn (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 linemy_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.loadsmethod Marshmallow framework returns aUnmarshalResultwhich is anamedtuple. So you can do:or
In other cases you may return a
dictfrom your function:But you might want consider to return an instance of a utility class (or a Pydantic/dataclass model instance), which wraps your data: