next() in Python

Liked I mentioned in my previous post, I have digging deep into list comprehensions and iterators in Python recently. During this exploration I came across a neat method possessed by all iterators, called next().

Syntax: next(iterable)

This method returns the next element of an iterable if it exists or else it throws an exception.

This method helped me finally realize that internally an for loop in python runs quite similarly to a while loop which uses the next() method and some error handling.

Leave a comment

Your email address will not be published. Required fields are marked *