Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

put-back-iterator

A drop-in replacement for iter(iterable) that allows putting items back after consuming them. Ideal for parsing, lexing, and other scenarios requiring lookahead or backtracking.

Why Use This?

Python's built-in iterators don't support pushing items back, forcing workarounds like:

  • Rebuilding iterators with itertools.chain
  • Using lists/deques as buffers
  • Complex state tracking

This package solves it with:

  • ✅ Simple API: .put_back(item) and .has_next()
  • ✅ Compatibility with iter(iterable): Works with any iterable (str, list, generators, etc.)
  • ✅ Python 2/3 compatible

Quick Start

# coding=utf-8
from __future__ import print_function
from put_back_iterator import PutBackIterator

# Wrap any iterable
it = PutBackIterator([1, 2, 3])

# Consume an item
print(next(it))  # 1

# Put it back for later
it.put_back(1)

# Check if more items exist (without consuming)
print(it.has_next())  # True

# Iterate as usual
print(list(it))  # [1, 2, 3]

Use Cases

1. Parsing/Tokenizing

tokens = PutBackIterator(tokenize(source_code))
while tokens.has_next():
    token = next(tokens)
    if token == 'IF':
        # Peek ahead without consuming
        if tokens.has_next() and next(tokens) == 'EOF':
            tokens.put_back('EOF')
            handle_incomplete_if()

2. Backtracking Algorithms

def backtrack(it):
    item = next(it)
    if not is_valid(item):
        it.put_back(item)  # Try alternative path
        return fallback()

3. Stream Processing

for chunk in PutBackIterator(stream):
    if needs_retry(chunk):
        chunk = modify(chunk)
        it.put_back(chunk)  # Reprocess

API Reference

PutBackIterator(iterable) Wraps an iterable.,Compatible with iter(iterable).

Methods

  • .put_back(item: T) -> None Pushes an item back into the iterator (LIFO order).
  • .has_next() -> bool Returns True if more items exist (peeks without consuming).
  • .__iter__() and .__next__() / .next() Compatible with Python 2/3 iterator protocol.

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Release files for put-back-iterator 0.1.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for put-back-iterator 0.1.0a1
File Size Uploaded
put_back_iterator-0.1.0a1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for put-back-iterator 0.1.0a1
File Interpreter ABI Platform
put_back_iterator-0.1.0a1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 9.3 kB

Release files / put_back_iterator-0.1.0a1.tar.gz

Download URL put_back_iterator-0.1.0a1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9a721b36068a962b4b42c62c65fa241aa0962b645054d2838a2c2c6c2f7c3620
BLAKE2b-256 checksum
How to use checksums
a2a55bdedc17a9a87b3dda4b012bb411608760a173d5928899299177782e15da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / put_back_iterator-0.1.0a1-py2.py3-none-any.whl

Download URL put_back_iterator-0.1.0a1-py2.py3-none-any.whl
Size 4.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
28975f85ae60f1ba5b717db0dfa4d903864fe5b7e54ded61df627ecba242c584
BLAKE2b-256 checksum
How to use checksums
e8dd8ff58613fef752e448963dd9f79d359ebf991d135f21e098b8cce7f0ba48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.1.0a1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page