BlogsDope image BlogsDope

next() in Python

March 26, 2021 FUNCTION PYTHON 245

To learn about the next() function in Python, we should be familiar with iterables in Python. Let's learn in brief about them.

Iterables in Python

An iterable is a Python object that can be used as a sequence. They are used to store multiple items in a single variable. Iterables in Python are List, tuple, Set, Dictionary. Let's see an example of each:

# a list
a = [1, 2, 3, 4]

# a set
b = ("cookies", "candies", "toffee")

# A set
c = {20, 30, 40}

# A dictionary
d = {
    "id": 1,
    "name": "Amit"

To access each element of any of the sequence, we need to itearte over it. Python allows us to iterate in many ways. Some of them are using a for loop, while loop, next(), iter(), list comprehension, etc. here, we will see how next() function is used to iterate in Python.

What is next() in Python?

Itearbles in Python are List, Tuple, String, Dictionary, Set, etc and an Iterator is an object that can be iterated over an Iterable. This can be achieved using the next() function in Python  that returns the next item of the iterble object. It is used to get or fetch the next item of the iterable. Let's see the syntax of next()

Syntax of next()

next(iterable, default)
An iterable object like List, Set, Tuple, etc
A default value to return when the iterable has exhausted (reached the end) or when the iterable is empty

iter() in Python

One important thing to be considered that the next() function in Python cannot be used directly on an iterable like List or Tuple. These have to be first converted into an iterator object which is achieved by using the function iter(). The iter() function takes an iterable like a List as a parameter and returns an iterator object. It simply converts an iterable to an iterator. Let's see the return value of iter()

a = iter([1, 2, 3, 4])

<class 'list_iterator'>

Examples of next()

a = iter([1, 2, 3, 4])





We can see in the above example that first we have converted the List into an iterator object using iter() and then using the next() function in Python we can iterate over it and fetch the next element. Should we see what happens if we don't use the iter() function? Check the below code:

a = [1, 2, 3, 4]

​TypeError: 'list' object is not an iterator

We can see we got an TypeError that the list is not an iterator object.

Now, we know that we have total of 4 items in the list. What if we try to use the next() function further after the forth item? Let's see:

a = iter([1, 2, 3, 4])
print(next(a))  # 1
print(next(a))  # 2
print(next(a))  # 3
print(next(a))  # 4
print(next(a))  # error



We got an StopIteration error if we try to fetch any item further after the end of the list. We can overcome this using a second parameter of the next() function that is the default value. If we provide the default value, then instead of the StopIteration error, the default value gets printed. Let's see an example of the same:

a = iter(["Delhi", "Pune", "Jaipur", "Udaipur"])
print(next(a, "No city"))
print(next(a, "No city"))
print(next(a, "No city"))
print(next(a, "No city"))
print(next(a, "No city"))





No city

Liked the post?
Rarely seen, always noticed.
Editor's Picks

Please login to view or add comment(s).