What causes the TypeError: 'NoneType' object is not iterable in Python?

I’m getting a TypeError: ‘NoneType’ object is not iterable when trying to loop over a variable using a for loop. For example:

for row in data: print(row) What does this error mean, and how can I fix it?

Ah, this one bit me early in my career. I had a function,def fetch_data(), that was supposed to return a list, but I forgot to actually return anything. So when I tried looping over the result like this:

data = fetch_data()
for row in data:
    print(row)

boom! Got hit with the typeerror: ‘nonetype’ object is not iterable. The function was returning None by default since there was no return statement. A simple return my_list fixed it. It’s a reminder to always check that your functions are actually returning what you expect, especially when dealing with loops.

Yep, I’ve tripped over this too, especially when messing with lists inside loops. My classic mistake? Doing this:

data = data.append(new_item)

Thing is, .append() modifies the list in place and returns None. So I ended up assigning None to my list. Took me a bit to spot that subtle mistake. So when I tried iterating later, Python yelled at me with a **typeerror: ‘nonetype’ object is not iterable

The fix? Just remove the assignment:

data.append(new_item)

Always double-check where you’re modifying lists—especially before using them in a loop.

That’s relatable, and here’s a twist, mine happened while chaining methods. I had something like:

data = get_items().filter(condition).sort()

Turned out, my custom filter() method was returning None instead of the expected list. So when the next method tried chaining on None, Python threw the typeerror: ‘nonetype’ object is not iterable again.

What helped? Breaking it down step-by-step and checking each part:

items = get_items()
filtered = items.filter(condition)
data = filtered.sort()

Once I found where the None was sneaking in, I fixed the method to always return the correct value. Lesson learned: don’t trust long chains unless you’re sure each link holds up.