Today I learned: comparison of Python tuples

Turns out Python has built-in ordering for tuples and other built-in sequences, and it is organized very intuitively. I should have known that, but I didn’t 🙂 This makes comparing versions, etc. very intuitive.

(2,2) < (3,1) ==> True
(2,2) < (1,10) ==> False
(3,) < (3,10) ==> True
(3,) < (3,-1) ==> True

Docs: https://docs.python.org/3/reference/expressions.html#value-comparisons.

Collections that support order comparison are ordered the same as their first unequal elements (for example, [1,2,x] <= [1,2,y] has the same value as x <= y). If a corresponding element does not exist, the shorter collection is ordered first (for example, [1,2] < [1,2,3] is true).

Leave a Reply

Your email address will not be published. Required fields are marked *