TypeError: object of type 'NoneType' has no len() (Python) — how to fix
Traceback (most recent call last):
File "app.py", line 9, in <module>
print(len(items))
TypeError: object of type 'NoneType' has no len()
Quick fix: Find where the variable was assigned. Don't assign the result of in-place methods (items.sort() returns None; use items = sorted(items)). Make sure your function ends with return value. Guard optional values: len(items or []) or if items is not None:.
What it means
len(x) only works on objects with a length (str, list, tuple, dict, set, ...). Here x is None. The len() line is just where it blows up; the real bug is wherever None was assigned. Trace the variable back:
print(repr(items)) # None
Cause 1: assigning the result of an in-place method
list.sort(), list.append(), list.extend(), list.reverse(), dict.update(), random.shuffle() change the object in place and return None:
items = [3, 1, 2]
items = items.sort() # items is now None
len(items) # TypeError
Fix: don't reassign, or use the function that returns a new object:
items.sort() # in place
items = sorted(items) # new list
Cause 2: a function without return
A function that reaches its end without return returns None:
def load_users(path):
with open(path) as f:
users = f.read().splitlines()
# forgot: return users
users = load_users("users.txt")
len(users) # TypeError
Also check every branch. If return is inside an if, the other path returns None:
def find(items, key):
if key in items:
return items[key]
return [] # explicit fallback
Cause 3: lookups that return None on a miss
dict.get(), re.match(), re.search(), os.environ.get(), ORM .first() and BeautifulSoup .find() return None when nothing is found:
tags = config.get("tags") # None if the key is missing
tags = config.get("tags", []) # default instead
m = re.search(r"\d+", text)
if m:
print(len(m.group()))
Cause 4: optional parameters
def process(rows=None):
for i in range(len(rows)): # TypeError when called as process()
...
Normalise at the top:
def process(rows=None):
rows = rows or []
Use if rows is None: rows = [] if an empty-but-valid value must be kept distinct.
Cause 5: API or JSON fields that can be null
null in JSON becomes None in Python:
data = r.json()
len(data["results"]) # fails if "results": null
results = data.get("results") or []
Cause 6: pandas / numpy
df.dropna(inplace=True), df.drop(..., inplace=True), df.sort_values(..., inplace=True) return None. Don't combine inplace=True with assignment:
df = df.dropna() # right
df = df.dropna(inplace=True) # df is now None
Safe pattern
count = len(items) if items is not None else 0
But prefer fixing the source of None, so bugs don't get hidden. Type hints plus mypy catch many of these: a function annotated -> list[str] with a missing return is flagged before you run it.
Last updated 2026-11-14.