Search code examples
python-3.xfunctionreturn-value

How to return multiple values as part of a list in Python


I have this function to reposition coordinates to draw objects to my screen

>>> def relative_pos(x, y):
...     # do something with x, y
...     return x, y

>>> rectangle = relative_pos(1, 2), relative_pos(3, 4)
>>> rectangle
((1, 2), (3, 4))

but I need the function to return the values without a tuple like this

>>> rectangle
(1, 2, 3, 4)

I can't figure out how to do this.


Solution

  • Just concatenate the tuples:

    >>> rectangle = relative_pos(1, 2) + relative_pos(3, 4)
    >>> rectangle
    (1, 2, 3, 4)