How to Sort a List of Tuples by the Second Element in Python
To sort a list of tuples by the second element in Python, pass a key function to sorted() or list.sort(): sorted(data, key=lambda t: t[1]). Add reverse=True for descending order, or use operator.itemgetter(1), which does the same thing slightly faster. This guide shows the basic sort, sorting by the second element and then another element, mixed … Read more >>









