Skip to content

Commit 01298dc

Browse files
authored
Improve documentation for pairwise_iteration_tee
Updated documentation to clarify the use of pairwise iteration and added a reference to the itertools.pairwise function.
1 parent 9ac2f25 commit 01298dc

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎data_structures/arrays/pairwise_iteration.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
55
Implementation of pairwise iteration algorithms, which can be useful in
66
many domains.
7-
Currently there are two different implementations.
8-
7+
Currently, there are two different implementations.
98
"""
109

1110
from collections.abc import Iterable, Iterator
12-
from itertools import tee
11+
from itertools import pairwise, tee
1312

1413

1514
def pairwise_iteration_tee(iterable: Iterable) -> Iterator[tuple]:
1615
"""
17-
Generate pairs of elements from an iterable.
16+
Generate pairs of elements from an iterable just like:
17+
https://docs.python.org/3/library/itertools.html#itertools.pairwise
1818
1919
This function uses the `tee` function from the `itertools` module to
2020
create two independent iterators (`a` and `b`) from the input
@@ -47,6 +47,16 @@ def pairwise_iteration_tee(iterable: Iterable) -> Iterator[tuple]:
4747
4848
>>> list(pairwise_iteration_tee(['a']))
4949
[]
50+
51+
>>> all(pairwise_iteration_tee(test) == pairwise(test)
52+
... for test in (
53+
... [1, 2, 3],
54+
... (4, 3, 5),
55+
... {'x':3, 'y':1, 'z':2, 'foo':4},
56+
... '2345',
57+
... ['ATG','GCT','TGC','TAA']
58+
... []
59+
... )
5060
"""
5161
# Uses itertools.tee to create two independent iterators (a and b)
5262
# from the iterable. This means we can use next() on each one

0 commit comments

Comments
 (0)