how to sort a tuple in python 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: sort by tuple
sorted_by_second = sorted(data, key=lambda tup: tup[1])
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
Example 4: how to sort tuples in python
a = (3,1,2)
a = list(a)
a.sort()
print(a)
>>> [1,2,3]
a = [3, 6, 8, 2, 78, 1, 23, 45, 9]
print(sorted(a))
>>> [1, 2, 3, 6, 8, 9, 23, 45, 78]