TypeError: object of type 'int' has no len()
Exception: TypeErrorCategory: runtimeRaised at: the len() call, when the argument has no length
What this error means
len() only works on things that contain items — strings, lists, tuples, dicts, sets, ranges. A number contains nothing, so it has no length and Python says so. The message names the actual type, so the same bug reads 'float' object has no len(), 'bool' object has no len() or 'NoneType' object has no len().
Traceback (most recent call last):
File "demo.py", line 3, in <module>
print(len(total))
^^^^^^^^^^
TypeError: object of type 'int' has no len()
Cause 1 — counting the digits of a number
The intent is usually 'how many digits does this number have?' That is a real question, but len() cannot answer it directly — convert to a string first, or use arithmetic:
total = 4821
len(total) # TypeError
len(str(total)) # 4 — the simple, readable fix
len(str(abs(total))) # 4 — ignores the minus sign
digits = 0 # arithmetic version, no conversion
n = abs(total)
while n:
n //= 10
digits += 1
Cause 2 — a variable that changed type
Very often the variable was a list and something reassigned it to a number. The len() line is correct; the mistake is upstream:
scores = [90, 85, 72]
print(len(scores)) # 3 — fine
scores = sum(scores) # scores is now the number 247
print(len(scores)) # TypeError
Cause 3 — a function returned a count instead of a collection
A function named get_items that returns len(items), or a database call that returns a row count, leaves the caller holding an integer:
def active_users(rows):
return len([r for r in rows if r["active"]]) # returns a number
users = active_users(data)
if len(users) > 0: # TypeError — users is already a count
...
if users > 0: # what was meant
Cause 4 — len() on a generator or a single value
Generators, iterators, map, filter, zip and enumerate objects are not sized, so len() refuses them too. Materialise them first:
squares = (n * n for n in range(5))
len(squares) # TypeError: object of type 'generator' has no len()
squares = list(squares) # or [n * n for n in range(5)]
len(squares) # 5
How to debug it
- Print
type(value)right before thelen(). The message already told you the type — this confirms where it came from. - Search backwards for an assignment to that name. A reassignment from a collection to a number is the most common story.
- If you wanted the digit count, use
len(str(abs(n))). - If the value came from a function, read what it returns on every path — a count and a collection are easy to confuse when both are named
totalorresult. - If it is a generator or iterator, wrap it in
list()— but only if you actually need the length; otherwise iterate it and count as you go.
Fix it interactively
These free problems practice the length and type rules — repair them in the browser:
Practice more Python bugs →Related messages worth knowing
- 'int' object is not iterable — the same number, met by a for loop instead of len().
- 'int' object is not subscriptable — the same number, met by square brackets.
- 'int' object is not callable — the same number, met by parentheses.
🇮🇳 Hindi में समझें
TypeError: object of type 'int' has no len() ka matlab hai aapne kisi number par len() lagaya. len() sirf un cheezon par chalta hai jinke andar items hote hain — string, list, tuple, dict, set, range. Number ke andar kuch hota hi nahi, isliye uski length bhi nahi hoti. Agar aap digits गिनना chahte the to pehle string me badlo: len(str(abs(n))). Doosri common wajah: variable pehle list tha, phir kisi line ne use number bana diya (jaise scores = sum(scores)) — galti len() wali line me nahi, usse upar hai. Teesri: function ne collection ki jagah count return kar diya. Chauthi: generator, map, zip jaise iterators par bhi len() nahi chalta — pehle list() me badlo.