Python 101 : Using the "next()" function to exit a loop - reading from a file -


We use the StopIteration error in order to end an iteration as below:


Sometimes we want to instruct the next() method to return a value that would tell us to end the iteration.

An example of these value is the "None" value, as we can see below: 


The next() function will add the "None" when it reaches the end of the file, so we can break out of the loop.

Comments

Leave as a comment:

Archive