Skip to content

Commit 2c5150c

Browse files
committed
More pythonic chunked() (issue #24)
1 parent d368747 commit 2c5150c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

unpythonic/it.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,13 @@ def chunked(n, iterable):
688688
raise ValueError("expected n >= 2, got {}".format(n))
689689
it = iter(iterable)
690690
def chunker():
691-
while True:
692-
chunk_it = islice(it, n)
693-
try:
694-
first_el = next(chunk_it)
695-
except StopIteration:
696-
return
697-
yield scons(first_el, chunk_it)
691+
try:
692+
while True:
693+
cit = islice(it, n)
694+
# we need the next() to see the StopIteration when the first empty slice occurs
695+
yield scons(next(cit), cit)
696+
except StopIteration:
697+
return
698698
return chunker()
699699

700700
def within(tol, iterable):

0 commit comments

Comments
 (0)