How to Fix: TypeError: object of type ‘int’ has no len()

3D illustration of a robot failing to use a tape measure on a single steel ball, representing the TypeError object of type int has no len.

The len() function returns the length (number of items) of a container (like a list, string, or dictionary). If you try to use it on an integer, you will get a TypeError int has no len message.

This error “TypeError int has no len” means: “You asked for the length of a single number. A number doesn’t have a length.”

⚡ Quick Fix: TypeError: object of type ‘int’ has no len() – Python len() on Integer, Digit Count, and Variable Overwrite Fix

You called len() on a plain integer — Python has no unambiguous definition of “length” for a number, so it refuses to guess.

# Fix 1 — Count digits: convert to string first
length = len(str(12345))  # Output: 5

# Fix 2 — Compare the value directly, no len() needed
score = 10
if score > 5:
    print("High score")

# Fix 3 — Variable overwritten: trace where your list became an int
my_data = [1, 2, 3]
my_data = 0        # This kills the list — len() crashes here
my_data = [1, 2, 3]  # Keep it a list
print(len(my_data))  # Output: 3

This tells you exactly where your boundary is — now read through the 3 root causes below to find which one broke your code and fix it permanently.

The Cause

You passed an integer to len().

Problem Code:

my_number = 12345
length = len(my_number) 
# CRASH! TypeError: object of type 'int' has no len()

Is the length 1? (It’s one number). Is the length 5? (It has 5 digits). Python refuses to guess.

The Fix 1: Did you want the number of digits?

If you want to know how many digits are in the number, convert it to a string first.

my_number = 12345
length = len(str(my_number)) # Convert "12345" -> length is 5
print(length)
# Output: 5

The Fix 2: Did you mean to check the value?

Sometimes beginners confuse length with value.

score = 10
# WRONG: if len(score) > 5:
# RIGHT:
if score > 5:
    print("High score")

The Fix 3: Did you overwrite a list?

Common mistake:

my_data = [1, 2, 3]
# ... later ...
my_data = 0 # Oops, overwrote the list with a number

print(len(my_data)) # CRASH!

Check where your variable was assigned!


What This Error Exposes About Python’s len() Protocol

TypeError: object of type 'int' has no len() is Python enforcing the __len__ protocol. Every object that supports len() — lists, strings, dictionaries, tuples — implements a __len__() method that returns an unambiguous item count. Integers implement no such method because “length” carries no single valid meaning for a scalar number. Five digits, one value, and zero items are all defensible answers, so Python picks none of them and raises the error instead.

The digit-counting fix works because str(12345) produces "12345" — a string of five characters with a perfectly unambiguous __len__ implementation. That conversion makes your intent explicit: you are measuring character count, not numeric magnitude. For large-scale digit counting across a dataset, math.floor(math.log10(abs(n))) + 1 is the arithmetic alternative that skips the string allocation entirely.

The variable overwrite scenario is the most dangerous trigger because the crash appears far from the actual mistake. A list assigned on line 3 and overwritten with an integer on line 47 raises the error at line 89 when len() finally runs — the traceback points at the symptom, not the cause. The diagnostic habit that catches this immediately: run type(my_variable) at the len() call site before assuming your variable holds what you expect. For production code, type hints and a static checker like mypy flag the overwrite at the assignment line and prevent the error from reaching runtime entirely.

Similar Posts

Leave a Reply