Menu
Coddy logo textTech

Iter() and Next() Functions

Lesson 2 of 13 in Coddy's Python Iterators course.

In Python, the iter() and next() functions are fundamental to working with iterators. Let's explore these functions and see how they're used to create and manipulate basic iterators.

The iter() Function

The iter() function is used to create an iterator object from an iterable. Its basic syntax is:

iter(iterable)

When called on an iterable, iter() returns an iterator object that can be used to access the elements of the iterable one by one.

The next() Function

The next() function is used to retrieve the next item from an iterator. Its basic syntax is:

next(iterator)

Each time next() is called on an iterator, it returns the next element in the sequence. If there are no more elements, it raises a StopIteration exception.

Using iter() and next()

Let's see how these functions work together:

# Create a list (an iterable)
fruits = ['apple', 'banana', 'cherry']

# Create an iterator from the list
fruit_iterator = iter(fruits)

# Use next() to get elements from the iterator
print(next(fruit_iterator))  # Output: apple
print(next(fruit_iterator))  # Output: banana
print(next(fruit_iterator))  # Output: cherry
# print(next(fruit_iterator))  # This would raise StopIteration

In this example, we create an iterator fruit_iterator from the fruits list using iter(). We then use next() to retrieve each element from the iterator.

Creating a Basic Custom Iterator

You can also create your own iterator by implementing the __iter__() and __next__() methods in a class:

class CountUp:
    def __init__(self, start, end):
        self.current = start
        self.end = end
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if self.current > self.end:
            raise StopIteration
        else:
            self.current += 1
            return self.current - 1

# Using the custom iterator
counter = CountUp(1, 3)
print(next(counter))  # Output: 1
print(next(counter))  # Output: 2
print(next(counter))  # Output: 3

This custom iterator CountUp counts up from a start number to an end number. The __iter__() method returns the iterator object itself, and the __next__() method defines how to get the next value.

Understanding iter() and next() is crucial for working with iterators in Python, allowing you to create and manipulate sequences of data efficiently.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create an iterator that generates a sequence of even numbers up to a given limit. Use the iter() and next() functions to implement this iterator.

You are provided with the following:

  • An integer limit as input

Your iterator should:

  • Generate even numbers starting from 2
  • Stop when it reaches or exceeds the given limit

Print each generated even number on a new line.

The input will be provided as a string representing an integer.

Try it yourself

# Read input
limit = int(input())

# TODO: Write your code here
# Implement the even number iterator using iter() and next()

# Example of how to print the result
# print(next_even_number)

All lessons in Python Iterators