Iterator vs iterable vs generator in Python

Backend Live Coding November 13, 2026 python

Iterable: has __iter__ and returns an iterator (list, dict, QuerySet).

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# ======================================================================
# Q: What is the difference between an iterator, an iterable and a generator?
#
# Iterable: has __iter__ and returns an iterator (list, dict, QuerySet).
# Iterator: has __next__ and is exhausted after one pass. Generator: the easiest
# way to build an iterator, using yield. Generator expression: (x * 2 for x in
# data), which does not create a list in memory.
# ======================================================================

def read_in_chunks(qs, size=2000):
    batch = []
    for obj in qs.iterator(chunk_size=size):
        batch.append(obj)
        if len(batch) == size:
            yield batch
            batch = []
    if batch:
        yield batch