Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
list comprehension for multiple return function?

I have a function that returns two values, and I would like use a list comprehension to fill two lists. for example:

def f(x):
  return 2*x,x*x

x = range(3)
xlist, ylist = [f(value) for value in x]

EDITS from answers below:
xtuple, ytuple = zip(*[f(value) for value in x])
xlist, ylist = map(list,zip(*[f(value) for value in x]))

where the expected return should be:

xlist = [0, 2, 4]
ylist = [0, 1, 4]

my question boils down to:

Currently I get a list of tuples, while this is reasonable I will end up needing two independent lists. currently I could have 1 placeholder (tuple list) variable, and 3 total comprehensions. But I'm wondering if there is a clean way to do it with as single list comprehension.

Worth noting: in the real code my two returns are correlated, so I cannot simply split the function into two.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

First of all, you made a small mistake: it should be:

[f(value) for value in x]
#  ^ notice the `value`

instead of:

[f(x) for value in x]

Furthermore the point is that:

return 2*x,x

is short for:

return (2*x,x)

so a tuple. Your list comprehension thus generates a list of tuples, not a tuple of lists. The nice thing of zip however is that you can easily use it in reverse with the asterisk:

xlist,ylist = zip(*[f(value) for value in x])
#                 ^ with asterisk

Note that xlist and ylist will be tuples (since zip will be unpacked). If you want them to be lists, you can for instance use:

xlist,ylist = map(list,zip(*[f(value) for value in x]))

which results in:

>>> xlist
[0, 2, 4]
>>> ylist
[0, 1, 4]

(note that ranges start counting from 0)

Alternative: Another way to do this is of course:

xlist = [f(value)[0] for value in x]
ylist = [f(value)[1] for value in x]

But this is of course inelegantly and furthermore can be inefficient (given f is computationally expensive).

over 4 years ago · Santiago Trujillo Denunciar

0

Let's make this work. The function is fine:

def f(x):
  return 2*x, x*x

But you want to define the range as follows, notice the starting and ending values:

x = range(1, 4)

Also, you have to call the function with the value, not with the list as parameter. And the final trick to unzip the result into two lists, is to simply zip(*lst) the result of the list comprehension:

xlist, ylist = zip(*[f(value) for value in x])

Now the result is as expected:

xlist 
=> [2, 4, 6]
ylist 
=> [1, 4, 9]
over 4 years ago · Santiago Trujillo Denunciar

0

Use the build-in function zip(),

def f(x):
  return 2*x, x*x

x = range(1, 4)
xlist, ylist = zip(*[f(value) for value in x])

print(xlist, ylist)
# ((2, 4, 6), (1, 4, 9))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda