There is a list:

[(2, 'hi'), (1, 'what'), (3, 'is'), (1, 'your'), (3, 'name'), (2, 'my'), (2, 'bond'), (1, 'james'), (4, 'damme'), (3, 'van'), (2, 'claude'), (1, 'jean')] 

You need to sort it by value, but if the value matches, then alphabetically. As far as I understand it is necessary to transfer a certain lambda-function to the key , but I do not know how to implement it.

    1 answer 1

     lst.sort(key = lambda x: (x[0], x[1])) 

    sorts by the first element stupid, taking into account the second, if the first match

    Result:

     [(1, 'james'), (1, 'jean'), (1, 'what'), (1, 'your'), (2, 'bond'), (2, 'claude'), (2, 'hi'), (2, 'my'), (3, 'is'), (3, 'name'), (3, 'van'), (4, 'damme')] 
    • But the tuple (1, 'what') must stand instead of the first element, 'W' occurs later than 'J'. - Mal Mel
    • Yes, what's alphabetically worth later j, and not before. What is the discrepancy? - MBo
    • It is necessary that the list be sorted by ascending and values ​​and, if required, alphabetically starting from the end of this alphabet, that is, it should be so, if you only write the first letters y, w, je, ja, my, hi and so on - Mal Mel
    • one
      The answer was given to a specific question in which nothing was said about “from the end” - MBo
    • one
      l.sort (key = lambda x: (-x [0], x [1]), reverse = True) - MBo