This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Muhammad.Alkarouri
Recipients Muhammad.Alkarouri, georg.brandl
Date 2010-01-23.13:52:57
SpamBayes Score 3.0078481e-06
Marked as misclassified No
Message-id <1264254781.04.0.3391109102.issue7764@psf.upfronthosting.co.za>
In-reply-to
Content
Based on the discussion at:
http://groups.google.co.uk/group/comp.lang.python/browse_thread/thread/c1ae3513a31eb63e/d0701a9902732c67?hl=en#d0701a9902732c67

In the documentation of itertools, one of the functions provided in the recipes section (10.7.3) is consume:

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    collections.deque(islice(iterator, n), maxlen=0)

A clearer implementation is:

def consume(n, items):
    if n == 0:
        return
    next(islice(items, n-1, None), None)

It uses no fancy tricks and is thus more suitable for a documentation. Moreover, the second implementation is actually more efficient. Some timings are provided in the thread linked above. As an example, here are the timings on my machine (Python 2.6.1, x86_64, OS X 10.6:

consume_deque #old implementation
    10: 1.2913839817
   100: 3.18093585968
  1000: 21.6316840649

consume_forloop #using a straight for loop
    10: 0.658184051514
   100: 2.85271406174
  1000: 24.6730420589

consume_islice #the suggested implementation
    10: 0.688861131668
   100: 1.15058612823
  1000: 5.52356886864

The script computing these timings is attached. Thanks to Peter Otten for coming up both with the function and the timing script.
History
Date User Action Args
2010-01-23 13:53:01Muhammad.Alkarourisetrecipients: + Muhammad.Alkarouri, georg.brandl
2010-01-23 13:53:01Muhammad.Alkarourisetmessageid: <1264254781.04.0.3391109102.issue7764@psf.upfronthosting.co.za>
2010-01-23 13:52:59Muhammad.Alkarourilinkissue7764 messages
2010-01-23 13:52:58Muhammad.Alkarouricreate