Take the 2-minute tour ×

What does error TypeError: 'NoneType' object is not iterable mean?

I am getting it on this code:

def write_file(data,filename): #creates file and writes list to it
  with open(filename,'wb') as outfile:
    for row in data:   ##i get the error here
share|improve this question

4 Answers 4

up vote 36 down vote accepted

It means "data" is None.

share|improve this answer

Code: for row in data:
Error message: TypeError: 'NoneType' object is not iterable

Which object is it complaining about? Choice of two, row and data. In for row in data, which needs to be iterable? Only data.

What's the problem with data? Its type is NoneType. Only None has type NoneType. So data is None.

You can verify this in an IDE, or by inserting e.g. print "data is", repr(data) before the for statement, and re-running.

Think about what you need to do next: How should "no data" be represented? Do we write an empty file? Do we raise an exception or log a warning or keep silent?

share|improve this answer

It means that the data variable is passing None (which is type NoneType), its equivalent for nothing. So it can't be iterable as a list, as you are trying to do.

share|improve this answer
it would be nice if just iterated like an empty list... would nake for cleaner code and less error checking –  deltanine Aug 19 '13 at 1:03
@deltanine It would make for a lot of problems being more difficult to detect I think. I'm glad None is different from an empty iterable. If you want your described behaviour, just use for row in data or []: –  Mark 20 hours ago

You're calling write_file with arguments like this:

write_file(foo, bar)

But you haven't defined 'foo' correctly, or you have a typo in your code so that it's creating a new empty variable and passing it in.

share|improve this answer

Your Answer


