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.
Python code
14 linesfrom 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
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
- Use `itertools.islice` with a while loop to paginate an infinite or iterator source.
- 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
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.