Python Iterators

Python · Functions & OOP

Theory

An iterator is an object that produces values one at a time and remembers where it is. It follows the iterator protocol: __iter__() returns the iterator itself and __next__() returns the next value, raising StopIteration when nothing is left. An iterable (a list, string, dict, file) is anything you can call iter() on to get a fresh iterator.

A for loop is built on this: for x in [1, 2, 3] calls iter() on the list, then keeps calling next() until StopIteration ends the loop quietly. Iterators are lazy - they compute or fetch each value only when asked - so you can walk through a huge file line by line without loading it all into memory. A generator function that uses yield is the easiest way to write your own iterator.

Why we use it

Iterators give one uniform way to loop over any kind of collection or stream, and because they are lazy they handle very large or even endless data using very little memory.

When to use it

Use iterators and generators to stream large files, database rows or computed sequences. Once consumed, an iterator is exhausted; if you need to reuse values or index them, convert to a list with list(it).

How it works

  1. it = iter([1, 2, 3])
  2. Call next(it)
  3. Any items left? Yes: Return the next item and move the position forward. No: Raise StopIteration.
  4. Repeat next(it) as needed
  5. A for loop catches StopIteration and stops

Syntax

iter(obj)
next(iterator)

Example

it = iter([1,2,3])
next(it)  # 1

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic