1sorted([('abc', 121),('abc', 231),('abc', 148), ('abc',221)], key=lambda x: x[1])
1# Python program to sort a list of tuples by the second Item
2
3# Function to sort the list of tuples by its second item
4def Sort_Tuple(tup):
5 # Getting length of list of tuples
6 lst = len(tup)
7 for i in range(0, lst):
8 for j in range(0, lst-i-1):
9 if (tup[j][1] > tup[j + 1][1]):
10 temp = tup[j]
11 tup[j]= tup[j + 1]
12 tup[j + 1]= temp
13 return tup
1# If you have a tuple, first convert it to a list:
2
3a = (3,1,2)
4a = list(a)
5
6# And now you can just use the inbuilt function ( .sort() ):
7a.sort()
8print(a)
9>>> [1,2,3]
10
11# ^ by using .sort() you are modifying the original list / tuple, however
12# if you want to make a sorted copy of the list / tuple, then you use this:
13
14a = [3, 6, 8, 2, 78, 1, 23, 45, 9]
15print(sorted(a))
16
17>>> [1, 2, 3, 6, 8, 9, 23, 45, 78]
18
19# This also works with the alphabet or a combination between letters and
20# numbers.