Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Simplify takewhile()
  • Loading branch information
rhettinger committed May 23, 2024
commit ed59bc4a706807acb105a1d4897bff0f90cad370
5 changes: 2 additions & 3 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,9 @@ loops that truncate the stream.
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) → 1 4
for x in iterable:
if predicate(x):
yield x
else:
if not predicate(x):
break
yield x

Note, the element that first fails the predicate condition is
consumed from the input iterator and there is no way to access it.
Expand Down