Give the sort() a key method:
>>> my_list = [(1, 2), (3, 4), (2, 1), (3, 2)] >>> my_list.sort(key=lambda x: x[1]) >>> my_list [(2, 1), (1, 2), (3, 2), (3, 4)] >>>
If you need to get a sorted list, instead of sorting in place, use the builtin on sorted()
>>> sorted(my_list, key=lambda x: x[1]) [(2, 1), (1, 2), (3, 2), (3, 4)]
source share