Split iterator into chunks in python

Code that splits an iterator into chunks of iterators in python. I found some code on the net to split the list into chunks, but I couldn't find the code to split the iterator into chunks so I wrote it.

If you want to split a list into chunks from the beginning, you can use an index, and it's easy to split iterators while listing them. However, in order to divide the iterator as iterator, the sub-iterator had to notify the base iterator of StopIteration, which made this method difficult. After all, I'm using a slightly unpleasant method.

What's unpleasant is that I'm using the list exhausted = [False] to pass values out of the subiter. If you normally have it as a bool value, you can not write from inside to outside, so we are using a list that is a variable object. The class is used in a situation similar to the reference implementation of itertools.groupby in the official python documentation. So it may be a python-like implementation.

The feature is that it becomes too huge when listed, and it can be easily used even in cases that affect performance.

def chunk(it, n):
    it = iter(it)
    exhausted = [False]
    def subiter(it, n, exhausted):
        for i in range(n):
            try:
                yield next(it)
            except StopIteration:
                exhausted[0] = True
    while not exhausted[0]:
        yield subiter(it, n, exhausted)

Recommended Posts

Split iterator into chunks in python
Python --How do you split a list into evenly sized chunks in Python?
Read PNG chunks in Python (class)
Rectangle area element split in Python
Learn the design pattern "Iterator" in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Iterator in MySQLdb
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Put text scraped in Python into Google Sheets
Split files when writing vim plugin in python
Split camel case string word by word in Python
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python