FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.7] Add a prepend() recipe to teach a chain() idiom (GH-6415) by miss-islington · Pull Request #6421 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Doc/library/itertools.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ which incur interpreter overhead.
"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 tabulate(function, start=0):
"Return function(0), function(1), ..."
return map(function, count(start))
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_itertools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,11 @@ def test_permutations_sizeof(self):
... "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)

Expand Down Expand Up @@ -2350,6 +2355,9 @@ def test_permutations_sizeof(self):
>>> 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')]

Expand Down

Back | FazBrowse Home | New Git URL