Sort a list of tuples by value and then alphabetically

后端 未结 2 550
旧巷少年郎
旧巷少年郎 2021-01-19 01:36

Bit of a python newbie, but I got the following list of tuples. I need to sort it by value and if the value is the same, solve ties alphabetically. Here\'s a sample:

2条回答
  •  旧时难觅i
    2021-01-19 02:27

    You can use the sorted function:

    sorted_by_medals = sorted(list_of_medals, key=lambda tup: (-tup[1], tup[0]))
    

提交回复
热议问题