[Raymond Bisdorff <[email protected]>] > 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. _______________________________________________ Python-Dev mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/ATYT7LDUZ4W422CFRDQJB7KHKSAFM674/ Code of Conduct: http://python.org/psf/codeofconduct/
