Sort tuple by key in Python 3

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 sort it by the key? I tried the following command:

lista_tupla_ordenada = lista_tupla.sort(key=lambda, x: x[1])

But it did not 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 resolve this?

PS: yes, I need every duplicate key in my tuple: P

    
asked by anonymous 17.07.2016 / 18:27

1 answer

1

It is already the most difficult, now it is only:
Increasingly order (smallest to largest):

lista_tupla_ordenada = sorted(lista_tupla)

Sort decreasingly (highest to lowest):

lista_tupla_ordenada = sorted(lista_tupla, reverse=True)
    
17.07.2016 / 18:39