I need to sort a list of tuples in Python by a specific tuple element, let's say it's the second element in this case. I tried
sorted(tupList, key = lambda tup: tup[1])
I have also tried
sorted(tupList, key = itemgetter(1))
'''i imported itemgetter, attrgetter, methodcaller from operator'''
but the list was returned the same both times. I checked
sorting tuples in python with a custom key
sortedon a list doesn't modify the original list. If you're doingsorted(seq); print(seq);, then you're not going to see any changes. Make sure you're doingnew_thing = sorted(seq); print(new_thing)or similar.tupList.sort(key=lambda t: t[1])