[Raymond Bisdorff <raymond.bisdorff@pt.lu>]
I fully agree with your point. By default, all the components of the tuple should be used in the comparison.
Yet, I was confused by the following result.
from operator import itemgetter L = [(1, 'a'), (2, 'b'), (1, 'c'), (2, 'd'), (3, 'e')] L.sort(key=itemgetter(0), reverse=True) L = [(3, 'e'), (2, 'd'), (2, 'b'), (1, 'c'), (1, 'a')]
Should the tuples comparison is in this case, I thought, not be solely based on the first tuple component?
I'm not sure what you're doing there. The last line in your example isn't showing the result of sorting, it's assigning a brand new list to `L`' Here the same thing, but changing the last line to show the result of sorting:
from operator import itemgetter L = [(1, 'a'), (2, 'b'), (1, 'c'), (2, 'd'), (3, 'e')] L.sort(key=itemgetter(0), reverse=True) L [(3, 'e'), (2, 'b'), (2, 'd'), (1, 'a'), (1, 'c')]
So stability does matter when comparing only the tuples' first components, and tuples with the same first component did retain their original order.