AppDividend
Latest Code Tutorials

Python next() Example | next() Function In Python

0

Python next() Example | next() Function In Python is today’s topic. The next() function returns the next item from an iterator. The next() function is used to fetch the next item from the collection. It takes two arguments an iterator and a default value and returns an element. This method calls on iterator and throws an error if no item is present. To avoid the error, we can set a default value.

Python next()

You can add a default return value, to return if the iterable has reached to its end.

The syntax of next() is following.

next(iterator, default)
  1. iterator: The next() retrieves next item from the iterator.
  2. default (optional): The default value is returned if the iterator is exhausted (no items left).

It returns an item from the collection.

Let’s see some examples of next() function to understand it’s functionality.

# app.py

data = ['eleven', 'mike', 'dustin', 'caleb', 'noah']

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator))

See the following output.

➜  pyt python3 app.py
eleven
mike
dustin
caleb
noah
➜  pyt

We got an error from the last statement in the above program if we tried to get the next element when no next element was available (iterator is exhausted).

In such cases, you can give the default value as the second parameter.

#Iterator With Default Value

See the following example in which, we define a Python list with only two elements, and then we use the next() function to add three more items and print in the output.

# app.py

data = ['eleven', 'mike']

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))
print(next(dataIterator, 'caleb'))
print(next(dataIterator, 'dustin'))
print(next(dataIterator, 'noah'))

See the following output.

➜  pyt python3 app.py
eleven
mike
caleb
dustin
noah
➜  pyt

#Python Tuple with next() method

Let’s define Python Tuple and apply the next() function on that tuple iterator and see the output.

# app.py

data = ('eleven', 'mike')

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))

See the output.

➜  pyt python3 app.py
eleven
mike
➜  pyt

#Python next() Function with Python iter() Function

We have used the Python iter() function used to convert an iterable to an iterator. 

See the following code example.

# app.py

data = iter(['millie', 'finn'])

dataIterator = iter(data)

print(next(dataIterator))
print(next(dataIterator))

See the output.

➜  pyt python3 app.py
millie
finn
➜  pyt

To note here that For loop is the better choice when printing the contents of the list than next().

The next() is the utility function for printing the components of the container of iter type.

Its usage is when the size of the container is not known, or we need to give a prompt when the list/iterator has exhausted.

The method next() is used when the file is used as the iterator, typically in the loop, the next() method is called repeatedly. This method returns a next input line or raises StopIteration when the EOF is hit.

Combining the next() method with other file methods like readline() does not work right. However, using the seek() to reposition the file to an absolute position will flush the read-ahead buffer.

Finally, Python next() Example | next() Function In Python is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.