[docs]defbatched(iterable:T.Iterable[T_ITEM],n:int,*,strict:bool=False,)->T.Iterator[list[T_ITEM]]:""" Batch data into lists of length *n*. If the number of items in *iterable* is not divisible by *n*: - The last batch will be shorter if *strict* is ``False``. - :exc:`ValueError` will be raised if *strict* is ``True``. >>> list(batched('ABCDEFG', 3)) [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']] Args: iterable: An iterable of items of type T n: Batch size (must be at least 1) strict: If True, raises ValueError if the last batch is incomplete Returns: Iterator yielding lists of items from the input iterable Raises: ValueError: If n < 1 or if strict=True and last batch is incomplete """ifn<1:raiseValueError("n must be at least one")iterator=iter(iterable)whilebatch:=list(islice(iterator,n)):ifstrictandlen(batch)!=n:raiseValueError("batched(): incomplete batch")yieldbatch