tuple Parameter unpacking is not supported in python 3

Asked

Viewed 121 times

0

I started studying Python now and came across this mistake

tuple parameter unpacking is not supported in python 3

Below is the excerpt of the code:

#cria uma lista(user_id, numero_de_amigos)

numero_de_amigos_by_id=[(usuario["id"], numero_de_amigos(usuario))for usuario in usuarios]
sorted(numero_de_amigos_by_id, key=lambda (usuario_id, numero_de_amigos):numero_de_amigos, reverse=True)

How do I resolve this? the error is in key=lambda (usuario_id, numero_de_amigos)

1 answer

2


You have a list of tuples, then in your lambda expression, which you defined in the parameter key, you will also receive a tuple as parameter. IE, is passed to the lambda a tuple with 2 positions and you waiting two parameters. Given this inconsistency the interpreter generates the cited error, which is not possible to deconstruct the tuple passed in two parameters in this context.

So instead of waiting for two parameters, you should receive only one, which is a tuple of two values, and access the desired value:

sorted(numero_de_amigos_by_id, key=lamba it: it[1], reverse=True)

Thus, it will be a tuple and you will be accessing position 1, referring to the value of numero_de_amigos(usuario).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.