Programming Search⌘K

Featurecore

Comparing sequences and other types

Also called lexicographical comparison, compare lists, compare tuples, tuple ordering, list equality, mixed type comparison, NaN comparison, compare dicts.

Lists, tuples and strings compare item by item in lexicographical order, and a shorter prefix is smaller. A list never equals a tuple, and ordering across unrelated types raises TypeError.

Language
Python

Synopsis

print((1, 2, 3) < (1, 2, 4), [1, 2, 3] < [1, 2, 4])
print("ABC" < "C" < "Pascal" < "Python")
print((1, 2, 3, 4) < (1, 2, 4), (1, 2) < (1, 2, -1))
print((1, 2, 3) == (1.0, 2.0, 3.0))
print((1, 2, ("aa", "ab")) < (1, 2, ("abc", "a"), 4))
Output
True True
True
True True
True
True

These are the examples from the tutorial. The first pair of items that differ decides the result.

Semantics

r1. "The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted."1

r2. "If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively."1 "If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one."1

r3. "Strings (instances of str) compare lexicographically using the numerical Unicode code points (the result of the built-in function ord()) of their characters."2 Example: case.

r4. "Sequences (instances of tuple, list, or range) can be compared only within each of their types, with the restriction that ranges do not support order comparison. Equality comparison across these types results in inequality, and ordering comparison across these types raises TypeError."2 Examples: cross-type, list-vs-tuple.

r5. "Mappings (instances of dict) compare equal if and only if they have equal (key, value) pairs."2 Sets "define order comparison operators to mean subset and superset tests. Those relations do not define total orderings".2 Example: sets-dicts.

r6. "The default behavior for equality comparison (== and !=) is based on the identity of the objects."2 "A default order comparison (<, >, <=, and >=) is not provided; an attempt raises TypeError."2

r7. "The built-in containers typically assume identical objects are equal to themselves."2 For NaN, "not-a-number values are not equal to themselves."2 Example: nan.

r8. "comparing objects of different types with < or > is legal provided that the objects have appropriate comparison methods. For example, mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the interpreter will raise a TypeError exception."1

Examples

print("apple" < "banana", "Zebra" < "apple", ord("Z"), ord("a"))
print(sorted(["b", "C", "a"]), sorted(["b", "C", "a"], key=str.lower))
Output
True True 90 97
['C', 'a', 'b'] ['a', 'b', 'C']

Upper case letters have lower code points than lower case letters (rule r3).

people = [("bob", 25), ("ann", 30), ("bob", 20)]
print(sorted(people))
print(max(people))
Output
[('ann', 30), ('bob', 20), ('bob', 25)]
('bob', 25)

Tuples sort by the first item, then by the second item (rule r1).

print({1, 2} < {1, 2, 3}, {1, 2} < {2, 3}, {2, 3} < {1, 2})
print({"a": 1, "b": 2} == {"b": 2, "a": 1})
print(range(0, 3) == range(0, 3, 1), [] == [])
Output
True False False
True
True True

{1, 2} and {2, 3} are neither smaller nor larger than each other (rule r5).

nan = float("nan")
print(nan == nan, [nan] == [nan])
Output
False True

The list comparison finds the same object in both places, so it treats the items as equal (rule r7).

Pitfalls

A list and a tuple with the same items are not equal:

This code runs but gives a wrong answer on purpose.

print([1, 2] == (1, 2))
Output
False

Convert one side first, for example tuple([1, 2]) == (1, 2) (rule r4).

Ordering between unrelated types is an error:

This code raises TypeError, on purpose.

print([1, "a"] < [1, 2])

The first items are equal, so Python compares "a" with 2, and that raises TypeError (rule r8).

Comparison

Go arrays and structs support == but not <. The Go spec says: "Array types are comparable if their array element types are comparable. Two array values are equal if their corresponding element values are equal."3 Go strings are ordered and "compared lexically byte-wise".3 Python orders lists and tuples too (rule r1), so sorted() works on a list of tuples with no extra code.

References

  1. The Python Tutorial, 5. Data Structures, 5.8. Comparing Sequences and Other Types
  2. The Python Language Reference, Python 3.15, 6.10.1. Value comparisons
  3. The Go Programming Language Specification, Comparison operators

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source