from jaraco itertools 6 3 0 wrap an iterable in an object that stores the count of items that pass through it items counterrange20 items count 0 values listitems items count 20 from moreitertools v8 13 0 from moreitertools v9 0 0 advance iterable by n steps if n is none consume it entirely efficiently exhausts an iterator without returning values defaults to consuming the whole iterator but an optional second argument may be provided to limit consumption i x for x in range10 nexti 0 consumei 3 nexti 4 consumei nexti traceback most recent call last file stdin line 1 in module stopiteration if the iterator has fewer items remaining than the provided limit the whole iterator will be consumed i x for x in range3 consumei 5 nexti traceback most recent call last file stdin line 1 in module stopiteration use functions that consume iterators at c speed feed the entire iterator into a zerolength deque advance to the empty slice starting at position n from jaraco itertools 6 3 0 wrap an iterable in an object that stores the count of items that pass through it items counter range 20 items count 0 values list items items count 20 from more_itertools v8 13 0 from more_itertools v9 0 0 advance iterable by n steps if n is none consume it entirely efficiently exhausts an iterator without returning values defaults to consuming the whole iterator but an optional second argument may be provided to limit consumption i x for x in range 10 next i 0 consume i 3 next i 4 consume i next i traceback most recent call last file stdin line 1 in module stopiteration if the iterator has fewer items remaining than the provided limit the whole iterator will be consumed i x for x in range 3 consume i 5 next i traceback most recent call last file stdin line 1 in module stopiteration use functions that consume iterators at c speed feed the entire iterator into a zero length deque advance to the empty slice starting at position n
import itertools
from collections import deque
from itertools import islice



class Counter:
    

    def __init__(self, i):
        self.count = 0
        self.iter = zip(itertools.count(1), i)

    def __iter__(self):
        return self

    def __next__(self):
        self.count, result = next(self.iter)
        return result



def always_iterable(obj, base_type=(str, bytes)):
    if obj is None:
        return iter(())

    if (base_type is not None) and isinstance(obj, base_type):
        return iter((obj,))

    try:
        return iter(obj)
    except TypeError:
        return iter((obj,))



def consume(iterator, n=None):
    
    
    if n is None:
        
        deque(iterator, maxlen=0)
    else:
        
        next(islice(iterator, n, n), None)