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.

4 Answers 4

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.

10
  • 7
    This can be misleading when talking about < and >. For example, (0, 1) < (1, 0) evaluates to True.
    – None
    Jul 30, 2015 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. Dec 8, 2015 at 21:41
  • 10
    @J.Money why do you think it can be misleading?
    – Don
    Dec 9, 2015 at 8:49
  • 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, 2016 at 8: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) Sep 22, 2016 at 15:41
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.

2
  • 1
    The page now linked from this answer does not seem to contain the text quoted.
    – plugwash
    Mar 10, 2020 at 16:09
  • I believe a better link to the quoted text is: 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. Nov 28, 2021 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.

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.

3
  • 3
    (1,2,3) > (1,2,2) gives True Mar 9, 2020 at 9: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, 2022 at 12:20
  • 1
    Your explanation that the comparison works as it does because2 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. Jan 18 at 10:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.