Let’s say that I have the following list:
l = [(1,2), (4,6), (5,1), (1,0)] l
[(1, 2), (4, 6), (5, 1), (1, 0)]
And I want to sort it by the second element of the tuple:
sorted(l, key=lambda t: t[1])
[(1, 0), (5, 1), (1, 2), (4, 6)]
If I want to sort it by the first element of the tuple:
sorted(l, key=lambda t: t[0])
[(1, 2), (1, 0), (4, 6), (5, 1)]