]> granicus.if.org Git - python/commitdiff
Issue 16774: Add a new itertools recipe (suggested by Alexey Kachayev).
authorRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 05:03:56 +0000 (22:03 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 05:03:56 +0000 (22:03 -0700)
Doc/library/itertools.rst

index 1f0bcedc0a4e9f6ec02ce4d86b506f045ba85e25..c5ba2eb5960cb7c43689efd05df9d377ea6f33c9 100644 (file)
@@ -662,6 +662,11 @@ which incur interpreter overhead.
        "Return function(0), function(1), ..."
        return map(function, count(start))
 
+   def tail(n, iterable):
+       "Return an iterator over the last n items"
+       # tail(3, 'ABCDEFG') --> E F G
+       return iter(collections.deque(iterable, maxlen=n))
+
    def consume(iterator, n):
        "Advance the iterator n-steps ahead. If n is none, consume entirely."
        # Use functions that consume iterators at C speed.