TypeError: '<' not supported between instances of 'str' and 'int' (tested fix)
TypeError: '<' not supported between instances of 'str' and 'int' means you compared a string with a number, usually a value from input() or a file that was never converted. We reproduced it, and the NoneType, dict and heapq variants, in Python 3.13 and tested each fix.
1. Ordering comparisons need compatible types
Python 3 refuses to order unrelated types with <, >, <= and >=. Python 2 allowed it and gave arbitrary answers. The message names the operator and both types, left side first:
age = "30" # what input(), CSV and sys.argv give you
for expr in ['age < 18', 'age > 18', '"a" <= 1', 'None < 1', '[1] < (1,)', '{1: 2} < {3: 4}']:
try:
eval(expr)
except TypeError as e:
print(f"{expr:16} TypeError: {e}")
print("age == 18 ->", age == 18) # == never raises: it is just False
Output:
age < 18 TypeError: '<' not supported between instances of 'str' and 'int'
age > 18 TypeError: '>' not supported between instances of 'str' and 'int'
"a" <= 1 TypeError: '<=' not supported between instances of 'str' and 'int'
None < 1 TypeError: '<' not supported between instances of 'NoneType' and 'int'
[1] < (1,) TypeError: '<' not supported between instances of 'list' and 'tuple'
{1: 2} < {3: 4} TypeError: '<' not supported between instances of 'dict' and 'dict'
age == 18 -> False
== never raises; comparing "30" == 18 is just False, which hides the same bug silently.
2. The fix for str vs int: convert the input
age = "30"
print(int(age) < 18)
Output:
False
input(), sys.argv, CSV cells and query strings are always strings. Convert with int() or float() before you compare (see invalid literal for int() if the conversion fails).
3. sorted(), max() and min() on mixed lists
data = [3, "10", 2]
for f in ["sorted(data)", "max(data)", "min(data)"]:
try:
eval(f)
except TypeError as e:
print(f"{f:13} TypeError: {e}")
print(sorted(data, key=int)) # compare as numbers
print(sorted(["10", "9", "2"])) # strings sort as text
print(sorted(["10", "9", "2"], key=int))
Output:
sorted(data) TypeError: '<' not supported between instances of 'str' and 'int'
max(data) TypeError: '>' not supported between instances of 'str' and 'int'
min(data) TypeError: '<' not supported between instances of 'str' and 'int'
[2, 3, '10']
['10', '2', '9']
['2', '9', '10']
max() reports '>' while sorted() and min() report '<'. Pass key=int to compare as numbers. Strings of digits sort as text ("10" before "2"), which is a silent bug of its own.
4. None in the list
scores = [7, None, 3]
try:
sorted(scores)
except TypeError as e:
print("TypeError:", e)
print(sorted(scores, key=lambda x: (x is None, x if x is not None else 0))) # None last
print(sorted(s for s in scores if s is not None)) # or drop None
Output:
TypeError: '<' not supported between instances of 'NoneType' and 'int'
[3, 7, None]
[3, 7]
5. heapq and priority tuples with a tie
import heapq
heap = []
try:
heapq.heappush(heap, (1, {"task": "a"}))
heapq.heappush(heap, (1, {"task": "b"})) # tie on priority -> compares the dicts
except TypeError as e:
print("TypeError:", e)
import itertools
heap, counter = [], itertools.count()
heapq.heappush(heap, (1, next(counter), {"task": "a"})) # tie-breaker that is always comparable
heapq.heappush(heap, (1, next(counter), {"task": "b"}))
print(heapq.heappop(heap))
Output:
TypeError: '<' not supported between instances of 'dict' and 'dict'
(1, 0, {'task': 'a'})
Tuples compare element by element, so equal priorities fall through to the dicts. Insert a counter as the second element so the comparison never reaches the payload. PriorityQueue and sorted() on tuples have the same problem. It is intermittent: we checked that two equal payloads, such as (1, {'a': 1}) twice, compare as equal tuples and raise nothing, so the crash only appears once a tie has different payloads.
6. Your own classes
class Job:
def __init__(self, p): self.p = p
try:
sorted([Job(2), Job(1)])
except TypeError as e:
print("TypeError:", e)
print([j.p for j in sorted([Job(2), Job(1)], key=lambda j: j.p)])
from dataclasses import dataclass
@dataclass(order=True)
class Job2:
p: int
print(sorted([Job2(2), Job2(1)]))
Output:
TypeError: '<' not supported between instances of 'Job' and 'Job'
[1, 2]
[Job2(p=1), Job2(p=2)]
Use a key= function, @dataclass(order=True), or define __lt__. Related: unsupported operand type(s).
Frequently asked questions
How do I fix '<' not supported between instances of 'str' and 'int'?
Convert the string first: int(value) < 18. Values from input(), argv and CSV files are always str.
Why does sorted() raise this error?
The list mixes types such as str and int, or contains None. Use key=int, or filter or replace None first.
Why does heapq say '<' not supported between instances of 'dict' and 'dict'?
Two items had the same priority, so tuple comparison reached the dicts. Add a counter: (priority, next(counter), item).
Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5: str/int, NoneType, list/tuple and dict comparisons, sorted/max/min on mixed lists, heapq ties and custom classes. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.