Skip to main content
Practice

Differences Between Iterables and Iterators

In Python, an iterable refers to any object, like a list, tuple, or string, that can return its elements one at a time and thus is considered a repeatable object.

Iterables can be used in for loops and with functions like list(), set(), and tuple().

Example of an Iterable
# A list is an iterable
my_list = [1, 2, 3]

# A list is a repeatable object
for item in my_list:
print(item)

On the other hand, an iterator is an object that allows us to traverse through all the elements of an iterable one element at a time.

An iterator can be created from an iterable using the iter() function, and we can access the next element using the next() function.

Example of an Iterator
# A list is an iterable
my_list = [1, 2, 3]

# Creating an iterator
my_iterator = iter(my_list)

# Access the next element
print(next(my_iterator)) # 1
print(next(my_iterator)) # 2
print(next(my_iterator)) # 3

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.