0
I have a lista_tupla = [(1, [1, 2, 3, 4]), (2, [5, 6, 7]), (3, [7, 8, 9]), (3, [10, 11, 12]), (4, [13, 14, 15])]
where the first index of each tuple is a key and the second index is a list as above.
I would like to know: how do I order it by key? I tried the following command:
lista_tupla_ordenada = lista_tupla.sort(key=lambda, x: x[1])
But it didn’t work. Return:
>>> lista_tupla_ordenada = lista_tupla.sort(key=lambda, x: x[1])
File "<stdin>", line 1
lista_tupla_ordenada = lista_tupla.sort(key=lambda, x: x[1])
^
SyntaxError: invalid syntax
How can I fix this?
PS: Yes, I need every duplicate key in my tuple :P
As the answer says, this case you don’t need the lambda - because you’re ordering straight through the first item of each tuple. But what’s wrong there is the comma after the word
lambda
the correct would be 'lambda x: x[0]- sem
,depois de lambda - e tomando o elemento
0(não elemento
1`) of each tuple.– jsbueno