pairwise#

pairwise(iterable, cyclic=False)[source]#

Return successive overlapping pairs taken from an input iterable.

Parameters:
iterableiterable

An iterable from which to generate pairs.

cyclicbool, optional (default=False)

If True, a pair with the last and first items is included at the end.

Returns:
iterator

An iterator over successive overlapping pairs from the iterable.

Examples

>>> list(nx.utils.pairwise([1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]
>>> list(nx.utils.pairwise([1, 2, 3, 4], cyclic=True))
[(1, 2), (2, 3), (3, 4), (4, 1)]