TypeError: '<' not supported between instances of 'str' and 'int'
1 · The lesson
readWhat 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
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:
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.
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.
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.
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.
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:
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:
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:
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:
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 likeif row[2] > 100. - A database query or API with missing values, where
max()orsorted()meets aNone. 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. Usekey=to compare only the first item.
Related errors
- ValueError: invalid literal for int() with base 10: the conversion itself failed, because the text isn't a whole number.
- ValueError: could not convert string to float: 'X': the same, for decimal numbers.
- TypeError: unsupported operand type(s) for +: 'int' and 'str': text and numbers mixed with
+instead of<. - TypeError: unsupported operand type(s) for +: 'NoneType' and 'int': a
Nonethat reached arithmetic instead of a comparison.
See Also
- All Python errors: the full index, by type and by when it happens.
- Conditionals (tutorial)
- Input and output
- Lists (tutorial)
- Operators
Found this useful? Share it with a friend who’s learning Python.