← Back to list

Iterators vs Iterables (Fruit-Basket Analogy)

We frequently become confused whenever we try to gain conceptual knowledge regarding iterators and iterables. Though both are related…

Kirti · 2025-08-18 18:16 · 2 claps · 1.6 min read
#iterators #iterables #iteration #iter #python-programming
Open on Medium ↗
Wiki topics: 💻 · Programming

Iterators vs Iterables

We frequently become confused whenever we try to gain conceptual knowledge regarding iterators and iterables. Though both are related concepts that are necessary for iteration, they differ conceptually and practically.

First, let’s review some fundamental formal definitions:

Iteration: Iteration refers to the process of traversing or going through each element of a collection, one by one. In programming, iteration usually happens with a loop or by repeatedly calling next() on an iterator.

Iterable: Iterable refers to those objects that can be iterated over, which means you can go through each item one by one.

Iterator: An iterator is an object that keeps track of the current iteration state and returns the next element in the sequence. It basically provides a method for iterating over an iterable.

I know that must be very confusing to read, so let’s try to understand it using a very easy example.

Imagine a fruit basket containing three fruits: Apple, Banana, and Orange, and you want to pick fruits one by one. You reach in and pick up an Apple, then a Banana, and finally the Orange. Easy, right?

  • In this case, you are traversing through each fruit (item) in the basket (collection of objects). This process is called iteration.
  • Here, the basket is considered an iterable, because you can go through its items.
  • Your hand, which points to each fruit, acts as an iterator. Each time you say “next,” your finger moves to the next fruit. The iterator remembers your current position, and when you reach the last fruit, there’s nothing left, so it stops.

That’s it! Now, relating these concepts to actual Python objects (iterables) such as lists, strings, tuples, etc. You can create an iterator from any of these using the iter() function.

Also remember “Every iterator is also an iterable because you can traverse it multiple times using iteration methods. However, not every iterable is an iterator.”

The iterator keeps track of your current position, and you can access items one by one using the next() function. When there are no items left, Python raises a StopIteration exception. This demonstrates how iterables and iterators work together to make traversing data structured, predictable, and easy to control.


메타데이터
post_id
57e7c5bb0d9d
slug
iterators-vs-iterables-fruit-basket-analogy-57e7c5bb0d9d
url
https://medium.com/@kirtibohra15/iterators-vs-iterables-fruit-basket-analogy-57e7c5bb0d9d
canonical_url
https://medium.com/@kirtibohra15/iterators-vs-iterables-fruit-basket-analogy-57e7c5bb0d9d
author_url
https://medium.com/@kirtibohra15
status
ok
fetched_at
2026-07-18 03:40:46