# return multiple values from fuction

Discussion in 'Python' started by Jay Dorsey, Nov 6, 2003.

1. ### Jay DorseyGuest

Lupe wrote:
> hi, if someone can help me I would be grateful
>
> when I do
>
> def function
> kjklj
> llklç
>
> return variableA, variableB
>
> how can I assign the two return values to two distinct variables, as for ex.
>
> varC = variableA
> varD = variableB
>

>>> def a():

.... return "value 1", "value 2"
....
>>> c, d = a()
>>> c

'value 1'
>>> d

'value 2'
>>> e = a()
>>> e

('value 1', 'value 2')

HTH

Jay

Jay Dorsey, Nov 6, 2003

2. ### LupeGuest

hi, if someone can help me I would be grateful

when I do

def function
kjklj
llklç

return variableA, variableB

how can I assign the two return values to two distinct variables, as for ex.

varC = variableA
varD = variableB

??

Lupe, Nov 6, 2003

3. ### Jay O'ConnorGuest

On Thu, 06 Nov 2003 19:58:55 +0000, Lupe <> wrote:

>hi, if someone can help me I would be grateful
>
>when I do
>
>def function
> kjklj
> llklç
>
> return variableA, variableB
>
>how can I assign the two return values to two distinct variables, as for ex.
>
>varC = variableA
>varD = variableB
>
>??
>
>

def test ():
...
return variableA, variableN

varC, vardD = test()

varC will contain variableA
varD ill contain variableB

Jay O'Connor, Nov 6, 2003
4. ### Irmen de JongGuest

Lupe wrote:
> how can I assign the two return values to two distinct variables, as for ex.

By using tuple unpacking:

(varC, varD) = function()

--Irmen

Irmen de Jong, Nov 6, 2003
5. ### Alex MartelliGuest

Lupe wrote:

> hi, if someone can help me I would be grateful
>
> when I do
>
> def function

this needs of course to be

def function():

> kjklj
> llklç
>
> return variableA, variableB
>
> how can I assign the two return values to two distinct variables, as for
> ex.
>
> varC = variableA
> varD = variableB

"just do it":

varC, varD = function()

Alex

Alex Martelli, Nov 6, 2003
6. ### Irmen de JongGuest

Alex Martelli wrote:

>>how can I assign the two return values to two distinct variables, as for
>>ex.
>>
>>varC = variableA
>>varD = variableB

>
>
> "just do it":
>
> varC, varD = function()

I like that comment... "just do it"...
I find this is also true for most other things
that you want to do in Python.

"How do I create a mapping between a person's last
name and the list of telephone numbers he/she can be
reached at?" -- "umm.. just do it?"

{ "de Jong": ['234234', '34562363'] }

or whatever ;-)

--Irmen

Irmen de Jong, Nov 6, 2003
7. ### LupeGuest

thank you

I'm starting with Python and I find it really great!

It's... natural!

Lupe

Lupe, Nov 6, 2003