pad

iteration_utilities.pad(iterable, fillvalue=None, nlead=0, ntail=0)

Pad the iterable with fillvalue in front and behind.

Parameters:

iterable : iterable

The iterable to pad.

fillvalue : any type, optional

The padding value. Default is None.

nlead, ntail : int or None, optional

The number of times to pad in front (nlead) and after (ntail) the iterable. If ntail is None pad indefinitly (not possible for nlead). Default is 0.

Returns:

padded_iterable : generator

The padded iterable.

Examples

>>> from iteration_utilities import pad, getitem
>>> list(pad([1,2,3], 0, 5))
[0, 0, 0, 0, 0, 1, 2, 3]
>>> list(pad([1,2,3], 0, ntail=5))
[1, 2, 3, 0, 0, 0, 0, 0]
>>> list(pad([1,2,3], 0, nlead=5, ntail=5))
[0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0]
>>> list(getitem(pad([1,2,3], 0, ntail=None), stop=10))
[1, 2, 3, 0, 0, 0, 0, 0, 0, 0]

Warning

This will return an infinitly long generator if ntail is None, so do not try to do something like list(pad([], ntail=None))!