diff -r 5dbe3c3d014b Doc/library/itertools.rst --- a/Doc/library/itertools.rst Thu Nov 20 15:04:31 2014 +0100 +++ b/Doc/library/itertools.rst Thu Nov 27 12:49:39 2014 -0800 @@ -104,7 +104,10 @@ # accumulate([1,2,3,4,5]) --> 1 3 6 10 15 # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120 it = iter(iterable) + try: total = next(it) + except StopIteration: + return yield total for element in it: total = func(total, element) @@ -405,7 +408,10 @@ def _grouper(self, tgtkey): while self.currkey == tgtkey: yield self.currvalue - self.currvalue = next(self.it) # Exit on StopIteration + try: + self.currvalue = next(self.it) + except StopIteration: + break self.currkey = self.keyfunc(self.currvalue) @@ -429,11 +435,14 @@ # islice('ABCDEFG', 0, None, 2) --> A C E G s = slice(*args) it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1)) + try: nexti = next(it) for i, element in enumerate(iterable): if i == nexti: yield element nexti = next(it) + except StopIteration: + pass If *start* is ``None``, then iteration starts at zero. If *step* is ``None``, then the step defaults to one. @@ -585,12 +594,15 @@ it = iter(iterable) deques = [collections.deque() for i in range(n)] def gen(mydeque): + try: while True: if not mydeque: # when the local deque is empty newval = next(it) # fetch a new value and for d in deques: # load it to all the deques d.append(newval) yield mydeque.popleft() + except StopIteration: + pass # iterator is exhausted return tuple(gen(d) for d in deques) Once :func:`tee` has made a split, the original *iterable* should not be