240

I have been reading the Core Python programming book, and the author shows an example like:

(4, 5) < (3, 5) # Equals false

So, I'm wondering, how/why does it equal false? How does python compare these two tuples?

Btw, it's not explained in the book.

nbro
  • 15,395
  • 32
  • 113
  • 196
Paulo
  • 6,982
  • 7
  • 42
  • 56

4 Answers4

264

Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal (i.e. the first is greater or smaller than the second) then that's the result of the comparison, else the second item is considered, then the third and so on.

See Common Sequence Operations:

Sequences of the same type also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length.

Also Value Comparisons for further details:

Lexicographical comparison between built-in collections works as follows:

  • For two collections to compare equal, they must be of the same type, have the same length, and each pair of corresponding elements must compare equal (for example, [1,2] == (1,2) is false because the type is not the same).
  • 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).

If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is considered smaller (for example, [1,2] < [1,2,3] returns True).

Note 1: < and > do not mean "smaller than" and "greater than" but "is before" and "is after": so (0, 1) "is before" (1, 0).

Note 2: tuples must not be considered as vectors in a n-dimensional space, compared according to their length.

Note 3: referring to question https://stackoverflow.com/questions/36911617/python-2-tuple-comparison: do not think that a tuple is "greater" than another only if any element of the first is greater than the corresponding one in the second.

Community
  • 1
  • 1
Don
  • 16,928
  • 12
  • 63
  • 101
  • 6
    This can be misleading when talking about `<` and `>`. For example, `(0, 1) < (1, 0)` evaluates to `True`. – None Jul 30 '15 at 14:17
  • 4
    @CMCDragonkai -- yes. try: `x = tuple([0 for _ in range(n)])` and do the same for y. Setting n=100, 1000, 10,000, and 100,000 and running `%timeit x==y` gave timing values of .5, 4.6, 43.9, and 443 microseconds respectively, which is about as close to O(n) as you can practically get. – Michael Scott Asato Cuthbert Dec 08 '15 at 21:41
  • 10
    @J.Money why do you think it can be misleading? – Don Dec 09 '15 at 08:49
  • @J.Money wait I'm confused. The answer seems to imply that they are compared element-wise but that example seems they are compared based to first position. – Charlie Parker Sep 22 '16 at 02:00
  • @Don to me its confusing because that example seems to imply that the comparison is based on the first element while your answer seems to imply they are compared element-wise. – Charlie Parker Sep 22 '16 at 02:00
  • 2
    @CharlieParker `<` and `>` do not mean "smaller then" and "greater then" but "comes before" and "comes after": so `(0, 1)` "comes before" `(1, 0)` – Don Sep 22 '16 at 08:09
  • 3
    @Don I guess its not clear to we what type of ordering to impose on a tuple. I guess python just treats it as numbers by checking the largest significant digit first and the moving on to break dies...(in an element wise fashion) – Charlie Parker Sep 22 '16 at 15:41
  • It IS less than. That's why `-2<-1`. The meaning isn't changed; tuples are just big-endian. The same as numbers: `01` IS less than `10`. – DimeCadmium Apr 07 '18 at 02:37
  • What does it mean "tuples must not be considered as coordinates in a n-dimensional space!"? – Dims Jul 03 '18 at 21:16
  • 2
    @J.Money It's very practical because tuples can be placed into more complex data structures like heap and be compared by first value. Interestingly enough you also get stable sort as a bonus in some cases. – Leo Ufimtsev Jul 13 '19 at 22:54
29

The Python documentation does explain it.

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • 1
    The page now linked from this answer does not seem to contain the text quoted. – plugwash Mar 10 '20 at 16:09
  • I believe a better link to the quoted text is: https://docs.python.org/3/reference/expressions.html#value-comparisons . One does need to scroll down a bit to find the quoted text, but with the given link one must scroll up, which is unexpected and most would probably not do that. – bartonstanley Nov 28 '21 at 20:48
1

The python 2.5 documentation explains it well.

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).

Unfortunately that page seems to have disappeared in the documentation for more recent versions.

plugwash
  • 9,724
  • 2
  • 38
  • 51
0

I had some confusion before regarding integer comparsion, so I will explain it to be more beginner friendly with an example

a = ('A','B','C') # see it as the string "ABC" b = ('A','B','D')

A is converted to its corresponding ASCII ord('A') #65 same for other elements

So, >> a>b # True you can think of it as comparing between string (It is exactly, actually)

the same thing goes for integers too.

x = (1,2,2) # see it the string "123" y = (1,2,3) x > y # False

because (1 is not greater than 1, move to the next, 2 is not greater than 2, move to the next 2 is less than three -lexicographically -)

The key point is mentioned in the answer above

think of it as an element is before another alphabetically not element is greater than an element and in this case consider all the tuple elements as one string.

Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
  • 3
    `(1,2,3) > (1,2,2)` gives `True` – Vishal Singh Mar 09 '20 at 09:26
  • ```(20,2) > (9,30)``` gives ```True```, but ```202``` is not > ```930```, so for integers, it's comparing by position, not just concatenation. – mherzog May 21 '22 at 12:20
  • 1
    Your explanation that the comparison works as it does because`2 is less than 3 lexicographically` is incorrect. Try again with numbers like 11 and 3. Each pair of elements by position are compared using the `__gt__` method for that data type. For example, while `11 > 2` is True for integers, `'11' > '2'` is False for strings. Likewise for tuples, `(11, 33) > (2, 4)` is True while `('11', '33') > ('2', '4')` is False. The word "lexicographic" usually means "treat all character strings as strings, even those that depict numbers". Tuple elements that aren't strings aren't treated as strings. – Chris Johnson Jan 18 '23 at 10:39