How to Paginate a List with a Generator in Python

Define a generator that yields list items in fixed-size pages, simulating pagination for cloud resource APIs.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 11 views 0 copies

Python code

14 lines
Python 3.9+
from typing import List, Iterator

def paginate_generator(items: List[str], page_size: int = 3) -> Iterator[List[str]]:
    """Yield items in fixed-size chunks with a mock pagination pattern."""
    for i in range(0, len(items), page_size):
        yield items[i:i + page_size]

if __name__ == "__main__":
    resources = ["user_001", "user_002", "user_003",
                 "post_101", "post_102", "post_103"]
    
    print("All resources:")
    for page_num, page in enumerate(paginate_generator(resources), start=1):
        print(f"  Page {page_num}: {page}")

Output

stdout
All resources:
  Page 1: ['user_001', 'user_002', 'user_003']
  Page 2: ['post_101', 'post_102', 'post_103']

How it works

The generator uses range(0, len(items), page_size) to step through the list in chunks. Each yield returns a slice items[i:i+page_size], producing one page at a time. Because it's a generator, the pages are produced lazily — memory stays low even for large resource lists. When iterating with enumerate, Python automatically retrieves each page and assigns a page number. This pattern is ideal for mocking cloud pagination (e.g., AWS or GCP resource listings).

Common mistakes

  • Using `return` inside the generator loop, which stops iteration after the first page.
  • Forgetting to use `page_size` in both the range step and the slice end, causing overlapping or missing items.
  • Assuming the final page is always full when the total length isn't divisible by the page size.

Variations

  1. Use `itertools.islice` with a while loop to paginate an infinite or iterator source.
  2. Add a `fetch_page(page_num)` function that mimics real API calls with offset-based pagination.

Real-world use cases

  • Mocking AWS S3 list_objects_v2 pagination in unit tests to avoid real network calls.
  • Building a CLI script that batches cloud instance IDs for resource tagging or deletion.
  • Simulating API pagination in a development environment when the backend isn't ready.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.