Answers for "sort tuple list by third element python"

1

how to sort a list by the second element in tuple python

# lists_of_tuples = [('item', 'price'), ('item', 'price'), ('item', 'price')]
def sort_prices(list_of_tuples): #sort the list b*y the price of each tuple
    list_of_tuples.sort(key=lambda x: x[1], reverse=True) #earse the "reverse" part to sort in small to big.
    return list_of_tuples, print(list_of_tuples)
Posted by: Guest on May-17-2020
0

python sort list according to two elements in tuple

sorted(unsorted, key=lambda element: (element[1], element[2]))
Posted by: Guest on May-23-2021

Code answers related to "sort tuple list by third element python"

Python Answers by Framework

Browse Popular Code Answers by Language