pad

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

Pad the iterable with fillvalue in front and behind.

Parameters:
iterableiterable

The iterable to pad.

fillvalueany type, optional

The padding value. Default is None.

nlead, ntailint or None, optional

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

Returns:
padded_iterablegenerator

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 infinitely long generator if ntail is None, so do not try to do something like list(pad([], ntail=None))!