Consume an iterable

Peter Otten __peter__ at web.de
Sat Jan 23 08:15:25 EST 2010


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)

Peter




More information about the Python-list mailing list