python order list of tuples code example
Example 1: pyhon sort a list of tuples
sorted([('abc', 121),('abc', 231),('abc', 148), ('abc',221)], key=lambda x: x[1])
Example 2: how to sort a list by the second element in tuple python
def sort_prices(list_of_tuples):
list_of_tuples.sort(key=lambda x: x[1], reverse=True)
return list_of_tuples, print(list_of_tuples)
Example 3: pyhon sort a list of tuples
def Sort_Tuple(tup):
lst = len(tup)
for i in range(0, lst):
for j in range(0, lst-i-1):
if (tup[j][1] > tup[j + 1][1]):
temp = tup[j]
tup[j]= tup[j + 1]
tup[j + 1]= temp
return tup