Consume an iterable

Peter Otten __peter__ at web.de
Sat Jan 23 14:46:16 CET 2010


Peter Otten wrote:

> Duncan Booth wrote:
> 
>> Peter Otten <__peter__ at web.de> wrote:
>> 
>>> With next(islice(...), None) I seem to have found a variant that beats
>>> both  competitors.
>>> 
>> It has different behaviour for n==0 but I'm sure that's easily fixed.
> 
> "Different behaviour" being a euphemism for broken ;)
> 
> def consume_islice(n, items):
>     if n == 0:
>         return
>     next(islice(items, n-1, None), None)

Even better:

def consume_islice(n, items):
    next(islice(items, n, n), None)

Peter



More information about the Python-list mailing list