Consume an iterable

Raymond Hettinger python at rcn.com
Sun Jan 24 14:57:01 EST 2010


>      def consume2(iterator, n):  # the approved proposal (see #7764)
>          if n is None:
>              collections.deque(iterator, maxlen=0)
>          else:
>              next(islice(iterator, n, n), None)

FWIW, the deque() approach becomes even faster in Py2.7 and Py3.1
which has a high-speed path for the case where maxlen is zero.
Here's a snippet from Modules/_collectionsmodule.c:

/* Run an iterator to exhaustion.  Shortcut for
   the extend/extendleft methods when maxlen == 0. */
static PyObject*
consume_iterator(PyObject *it)
{
	PyObject *item;

	while ((item = PyIter_Next(it)) != NULL) {
		Py_DECREF(item);
	}
	Py_DECREF(it);
	if (PyErr_Occurred())
		return NULL;
	Py_RETURN_NONE;
}


This code consumes an iterator to exhaustion.
It is short, sweet, and hard to beat.


Raymond



More information about the Python-list mailing list