TypeError: object of type 'int' has no len()
1 · The lesson
readWhat this error means
len() asks an object how many items it holds. Strings, lists, tuples, dicts, sets and ranges know the answer. A number doesn't hold items, so it has no length. None has no length either, and neither does a generator that hasn't produced its items yet.
The type in quotes tells you what you really passed. 'int' and 'float' mean a number. 'NoneType' means a missing value. 'generator', 'map', 'filter' or 'zip' mean a lazy iterator.
When you see it
Traceback (most recent call last): File "main.py", line 3, in <module> if len(pin) != 4: ~~~^^^^^ TypeError: object of type 'int' has no len()
Traceback (most recent call last): File "main.py", line 3, in <module> print(len(paid)) ~~~^^^^^^ TypeError: object of type 'generator' has no len()
Why it happens
Either the value isn't the type you think it is, or it is a type that can't know its length in advance.
- A number where you expected a string.
int(input()), a JSON field like"pin": 4821, or a pandas column of IDs read as integers.len(4821)is meaningless to Python, even though you can see four digits. Nonewhere you expected a list. A function that forgot to return, or a lookup that found nothing.- A generator or iterator.
map(),filter(),zip(),enumerate()and generator expressions produce items one at a time. They don't know how many items are left until they have run out, so they have no length.
orders = [{"id": "A1", "paid": True}, {"id": "A2", "paid": False}, {"id": "A3", "paid": True}]
paid = (o for o in orders if o["paid"])
print(len(paid)) # TypeError: object of type 'generator' has no len()How to fix it
To count the digits of an integer, convert it to a string first. Remove the minus sign, or it is counted as a character:
pin = 4821 print(len(str(pin))) # 4 balance = -1250 print(len(str(balance))) # 5 (the '-' counts as a character) print(len(str(abs(balance)))) # 4
If the value is a code that must keep its leading zeros (a PIN, a postcode, a phone number), it was never a number. Keep it as a string from the start: int("0042") is 42, and the zeros are lost permanently.
To count a generator, either consume it or build a list. Choose based on whether you need the items again:
orders = [{"id": "A1", "paid": True}, {"id": "A2", "paid": False}, {"id": "A3", "paid": True}]
# Only need the count: count as you go, no list kept in memory
print(sum(1 for o in orders if o["paid"])) # 2
# Need the count AND the items: make a list once
paid = [o for o in orders if o["paid"]]
print(len(paid), [o["id"] for o in paid]) # 2 ['A1', 'A3']Counting a generator uses it up. If you count it with sum(1 for _ in gen) and then loop over gen, the loop runs zero times, with no error.
For map() or filter(), wrap it in list():
names = map(str.title, ["ada", "grace", "linus"]) names = list(names) print(len(names), names) # 3 ['Ada', 'Grace', 'Linus']
For None, find out why the value is missing. If an empty result is valid, make the function return [] instead of None, and len() gives 0 with no special case.
When you'd see this in real code
- Validating a PIN or OTP that was converted with
int()before the length check. Check the length on the string, then convert. - Reading IDs from a CSV with pandas, which turns
"00731"into the integer731. Passdtype={"customer_id": str}toread_csv. - Porting Python 2 code, where
map()andfilter()returned lists andlen(map(...))worked. len(re.finditer(...)):finditeris lazy ('callable_iterator'in the message). Usere.findallif you only need a count.- Calling
len()on the result ofdict.get()when the key is missing ('NoneType').
Related errors
- TypeError: 'int' object is not subscriptable: the same mix-up of a number and a string, but with
[0]instead oflen(). - TypeError: 'NoneType' object is not iterable: a missing value where a list should be.
- TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
- AttributeError: 'int' object has no attribute 'X': a list or string method called on a number.
See Also
- All Python errors: the full index, by type and by when it happens.
- Generators: why they can't know their length.
- Strings (tutorial)
- Comprehensions
Found this useful? Share it with a friend who’s learning Python.