TypeError: object of type 'int' has no len() (tested fix)
TypeError: object of type 'int' has no len() means you called len() on something that isn't a container. We reproduced the int, float, NoneType, generator and map variants in Python 3.13 and tested each fix.
1. len() only works on sized containers
len() needs an object with a __len__ method: str, list, tuple, dict, set, range and so on. Numbers, None and lazy iterators don't have one:
for expr in ["len(12345)", "len(3.5)", "len(None)", "len(x for x in 'ab')", "len(map(str, [1]))", "len(range(3))"]:
try:
print(f"{expr:22}", eval(expr))
except TypeError as e:
print(f"{expr:22} TypeError: {e}")
Output:
len(12345) TypeError: object of type 'int' has no len()
len(3.5) TypeError: object of type 'float' has no len()
len(None) TypeError: object of type 'NoneType' has no len()
len(x for x in 'ab') TypeError: object of type 'generator' has no len()
len(map(str, [1])) TypeError: object of type 'map' has no len()
len(range(3)) 3
2. You wanted the number of digits
n = 12345
print(len(str(n))) # number of digits
print(len(str(-12345))) # careful: the minus sign counts
print(len(str(abs(-12345))))
Output:
5
6
5
3. len() on an element instead of the list
items = [4, 5, 6]
try:
for i in range(len(items[0])): # meant len(items)
pass
except TypeError as e:
print("TypeError:", e)
for i, x in enumerate(items): # usually you don't need len() at all
print(i, x)
Output:
TypeError: object of type 'int' has no len()
0 4
1 5
2 6
len(items[0]) asks for the length of the first element, an int. Usually enumerate() removes the need for range(len(...)) entirely.
4. NoneType has no len(): a function returned nothing
def find_rows(rows):
if rows:
return rows
# falls off the end -> returns None
try:
len(find_rows([]))
except TypeError as e:
print("TypeError:", e)
print(len(find_rows([]) or []))
Output:
TypeError: object of type 'NoneType' has no len()
0
A function that falls off the end returns None. Fix the function so it always returns a list, or fall back with or []. Methods like list.sort() also return None; see NoneType object has no attribute.
5. Generators, map and filter have no length
squares = (x * x for x in range(5))
print(len(list(squares))) # materialise first...
print(len(list(squares))) # ...but a generator can only be consumed once
print(sum(1 for _ in (x for x in range(5)))) # count without building a list
Output:
5
0
5
Converting to a list consumes the generator: the second len(list(squares)) is 0. Keep the list if you need it again, or count with sum(1 for _ in ...) when you only need the count.
6. Your own class
class Cart:
def __init__(self): self.items = ["a", "b"]
try:
len(Cart())
except TypeError as e:
print("TypeError:", e)
class Cart2(Cart):
def __len__(self): return len(self.items)
print(len(Cart2()), bool(Cart2()))
Output:
TypeError: object of type 'Cart' has no len()
2 True
Defining __len__ also makes empty instances falsy. Related: object is not iterable.
Frequently asked questions
How do I get the number of digits of an int?
len(str(abs(n))). Without abs(), the minus sign of a negative number is counted.
How do I fix object of type 'NoneType' has no len()?
Find what returned None (often a function with no return, or list.sort()) and return a list instead, or use len(x or []).
How do I get the length of a generator?
Convert it with list(gen) and keep the list, or count with sum(1 for _ in gen). Either way the generator is consumed.
Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5: len() on int, float, None, generator and map; digit counting; len of an element; functions returning None; consumed generators and __len__. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.