easy +10 pts

Pairwise Sequence Pairs

Implement a generator that yields consecutive overlapping pairs from any iterable.

Implement a generator function named `pairwise` that takes a single argument `iterable` (any iterable of items). It must yield consecutive overlapping pairs: `[item[0], item[1]]`, `[item[1], item[2]]`, and so on. Each yielded pair must be a **list** of two elements, not a tuple. If the iterable contains fewer than two items, the generator yields nothing. The input may be a list, tuple, string, dictionary (iteration yields keys), or any iterable. You must not use the built-in `itertools.pairwise` function.

Constraints

Input can be any iterable. The number of items is any non-negative integer. The function must be a generator (use `yield`). Do not import anything.

Example

>>> list(pairwise([1, 2, 3]))
[[1, 2], [2, 3]]
>>> list(pairwise('abcd'))
[['a', 'b'], ['b', 'c'], ['c', 'd']]
>>> list(pairwise([1]))
[]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Convert the iterable to a list first if you need indexing, or iterate with a pointer.
Remember to handle the case where the iterable has fewer than 2 items.
Use a loop that stops before the last item and yields the current and next item as a list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.