PythonMastery
reference 3 min read · lesson 64 of 80 in Errors

TypeError: '<' not supported between instances of 'str' and 'int'

1 · The lesson

read

What this error means

You asked Python whether one value is smaller or bigger than another, and the two values are types it can't put in order. Python can compare two numbers, or two strings, but it won't guess whether the text "17" is smaller than the number 18. The message names the operator you used (<, >, <= or >=) and the two types, in the order they were compared. The fix is almost always to make both sides the same type.

When you see it

python
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    if age < 18:
       ^^^^^^^^
TypeError: '<' not supported between instances of 'str' and 'int'

sorted(), max() and min() compare values for you, so the error can appear on a line with no < in it:

python
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(max(readings))
          ~~~^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'

Why it happens

Four situations cause almost every case.

1. A number from input() is still text. input() always returns a string, even when the reader types digits.

python
age = "17"                    # what input("Age? ") gives you
if age < 18:                  # TypeError: 'str' and 'int'
    print("Too young")

2. A list with mixed types. One value came from a file or a form and stayed text, while the others are numbers. sorted() fails as soon as it compares the two kinds.

python
scores = [90, "85", 72]
print(sorted(scores))         # TypeError: 'str' and 'int'

3. A None in the list. A function that returned nothing, or a missing reading, put None in the data. None can't be compared with anything.

python
readings = [3, None, 7]
print(max(readings))          # TypeError: 'NoneType' and 'int'

4. Values that have no order at all. Dicts, and objects of your own classes, can't be compared with < unless you tell Python how.

python
a = {"x": 1}
b = {"x": 2}
print(a < b)                  # TypeError: 'dict' and 'dict'

How to fix it

Convert where the value comes in. Turn text into a number on the line that reads it:

python
age = int("17")               # int(input("Age? ")) in a real program
if age < 18:
    print("Too young")        # Too young

Make the list one type before sorting:

python
scores = [90, "85", 72]
scores = [int(s) for s in scores]
print(sorted(scores))         # [72, 85, 90]

Leave out the None values, or decide where they go:

python
readings = [3, None, 7]
present = [r for r in readings if r is not None]
print(max(present))           # 7

Sort by a key that can be compared. For dicts, sort by one of their values. key= tells sorted() what to compare:

python
people = [{"name": "Grace", "age": 45}, {"name": "Ada", "age": 36}]
by_age = sorted(people, key=lambda p: p["age"])
print([p["name"] for p in by_age])   # ['Ada', 'Grace']

For your own classes, use key= the same way, or add a __lt__ method so Python knows how to order them. @dataclass(order=True) writes those methods for you.

When you'd see this in real code

  • A number guessing game, where guess = input(...) is compared with the secret number.
  • A CSV read with csv.reader, where every cell is text, then sorted or compared with a threshold like if row[2] > 100.
  • A database query or API with missing values, where max() or sorted() meets a None.
  • sorted() on a list of tuples, where the first items are equal and Python moves on to compare the second items, which are dicts or objects. Use key= to compare only the first item.

See Also

Found this useful? Share it with a friend who’s learning Python.