less than 1 minute read

  • An iterable is any object that can return an iterator and an iterator is the object used to iterate over an iterable object.

  • This implements iter and next methods.

  • This can interact with iterable objects like lists, tuples, dicts, and sets. An object which will return data, one element at a time.

Example:

my_obj = {"Python", "Iterator"}
iter_obj = iter(my_obj)
print(next(iter_obj))

# Output:
Iterator

Additional information

Iterator protocol

__iter__ - This method is called for the initialization of an iterator. This returns an iterator object.

__next__ - This next method returns the next value for the iterable.

References

Iterator

Example

Definition

Iterators basics

Python Iterator