Add a prepend() recipe to teach a chain() idiom (GH-6415) (GH-6421)
(cherry picked from commit 9265dd72e5ec1cfa5fcdb5be8ebffe1d9994bd4b)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index effd7f0..cbbb4c4 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -2198,6 +2198,11 @@
... "Return first n items of the iterable as a list"
... return list(islice(iterable, n))
+>>> def prepend(value, iterator):
+... "Prepend a single value in front of an iterator"
+... # prepend(1, [2, 3, 4]) -> 1 2 3 4
+... return chain([value], iterator)
+
>>> def enumerate(iterable, start=0):
... return zip(count(start), iterable)
@@ -2350,6 +2355,9 @@
>>> take(10, count())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+>>> list(prepend(1, [2, 3, 4]))
+[1, 2, 3, 4]
+
>>> list(enumerate('abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]