• Latest
  • Trending
Understanding Iterables vs Iterators in Python

Understanding Iterables vs Iterators in Python

January 28, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Friday, 17 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Understanding Iterables vs Iterators in Python

by ITECHNEWS
January 28, 2022
in Data Science, Leading Stories
0 0
0
Understanding Iterables vs Iterators in Python

Iterables and Iterators are often confused for one another, however, they are two distinct concepts. This article will explain the difference between the two, and how they are used. Let’s first briefly look at what Iteration is.

In layman’s terms, Iteration means ‘repeating steps’. In programming terms, Iterations is the repetition of a statement/block of code a specific number of times, producing an output one after another. Iterations can be executed by using for loops for example.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

 

Iterables in Python

Iterables are objects which can be looped/iterated over by using a for loop. Iterables contain data or have value, where executing a for loop will perform an iteration, producing outputs one after another.

An iterable implements the __iter__() method and returns an iterator object. However, if the __iter__() method is not defined, Python will use __getitem__() instead.

Examples of Iterables are lists, dictionaries, strings, tuples, and more. As long as you can loop over it, it is an Iterable.

To figure out whether an object is iterable or not, you will have to check if it supports __iter__. In order to do this, we use the dir() function, which returns all properties and methods of the specified object, excluding values.

Example Code:

class Person:
 name = "Nisha"
 age = 25
 country = "England"

print(dir(Person))

Output:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'__weakref__', 'age', 'country', 'name']

 

Iterators in Python

An Iterator is also an object which uses the __iter__() and __next__() methods, this is called the Iterator protocol. It is an iterable object with a state, meaning it remembers what stage it is at during iteration.

Iterators return values, one element at a time. As the next value of an Iterable object is being returned, the state of the iterator is updated and knows how to get to the next value using the __next__()method. Iterators can only move forward, they cannot go back or reset themselves.

Iterators also raise a StopIteration exception when there are no more elements or the object is exhausted.

Example Code:

Here I have created an iterator called ‘numbers’. I have put in a line of code to check its type. We are expecting the iteration to output all the numbers. However, I have asked it to print the 6th output, although there are only 5 values in the iterator. Let’s see what happens

numbers = iter([2, 4, 6, 8, 10])

print(type(numbers))

print(next(numbers))
print(next(numbers))
print(next(numbers))
print(next(numbers))
print(next(numbers))
# The next() function will raise StopIteration as it is exhausted
print(next(numbers))

Output:

We can see that the type is a ‘list_iterator’. The output stopped at 10 and raised a StopIteration as there were no more elements in the numbers list.

<class 'list_iterator'>
2
4
6
8
10

---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-15-0cb721c5f355> in <module>()
     11 
     12 # The bext() function will raise StopIteration as it is exhausted
---> 13 print(next(numbers))

StopIteration:

Limitations of Iterators:

  1. Iterators only move in a forward direction, they do not go backwards or reset.
  2. Iterators cannot be copied as it is a one-way object that can only move forward.
  3. Due to its one-way direction, there is no way to retrieve the previous element.

 

Similarities and Differences Between Iterables and Iterators

 

IterableIterator
Iterated by using:for loopfor loop
Methods used: __iter__()__iter__() and __next__()

 

An Iterator is an Iterable, as it also implements the __iter__() method.

Remember: Every Iterator is an Iterable, however not every Iterable is an Iterator.

Example of dir() of an Iterable:

numbers = [2, 4, 6, 8, 10]

print(dir(numbers))

Output:

In this output, I have highlighted __iter__, showing that it is a method of an Iterable.

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', 
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', 
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', 
'__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Example of dir() of an Iterator:

In this example, we are iterating the numbers list.

numbers = [2, 4, 6, 8, 10]
numbers2 = iter(numbers)

print(dir(numbers2))

Output:

In this output, I have highlighted __iter__ and __next__, showing that it is a method of an Iterator.

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', 
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', 
'__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', 
'__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', 
'__subclasshook__']

I hope this short blog has given you a better understanding of the differences between an Iterable and an Iterator.

Source: Nisha Arya, KDnuggets
Tags: Iterables vs Iterators
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version