Hoo boy, this was a big XY problem. You want to sort by the third character of the second element of each tuple. Use the sorted
built-in with its kwarg key
.
lst = [('abc', 'bcd'), ('abc', 'zza')]
result = sorted(lst, key=lambda t: t[1][2])
key
here accepts a function that is given each element in turn, and does something to it that is compared. In this case it returns the second element’s third element (t[1][2]
). Each element of the outer list is a tuple. Each tuple’s second element exists in index 1
, each second element’s third character exists in index 2
.
0
solved Sort by elements of a list of tuples [duplicate]