iterable_like

compas.itertools.iterable_like(target, reference, fillvalue=None)[source]

Creates an iterator from a reference object with size equivalent to that of a target iterable.

Parameters:
targetiterable

An iterable to be matched in size.

reference: iterable

The iterable containing the original data.

fillvalueobject, optional

A value to replace missing items.

Yields:
object

The next value in the iterator.

Notes

Values will be yielded one at a time until the reference iterable is exhausted. If target contains more values than reference, fillvalue will be used to cover the difference. Otherwise, only the same number of items from reference will be yielded as there would have been from target.

Examples

>>> keys = [0, 1, 2, 3]
>>> color = (255, 0, 0)
>>> list(iterable_like(keys, [color], color))
[(255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0)]
>>> list(iterable_like(color, keys))
[0, 1, 2]