itertools.ilen?

Terry Reedy tjreedy at udel.edu
Thu Aug 7 03:01:38 EDT 2003


"Jeremy Fincher" <fincher.*@osu.edu> wrote in message
news:bgsqj5$228$1 at news.cis.ohio-state.edu...
> Sometimes I find myself simply wanting the length of an iterator.

An iterator is a function/method that traverses (or possibly
generates) a seqeuence.  The sequence has a length (actual or
potential) but the iterator does not.

>  For example, to collect some (somewhat useless ;))
>  statistics about a program  of mine, I've got code like this:
>
>         objs = gc.get_objects()
>         classes = len([obj for obj in objs if inspect.isclass(obj)])
>         functions = len([obj for obj in objs if
inspect.isroutine(obj)])
>         modules = len([obj for obj in objs if
inspect.ismodule(obj)])
>         dicts = len([obj for obj in objs if type(obj) ==
types.DictType])
>         lists = len([obj for obj in objs if type(obj) ==
types.ListType])
>         tuples = len([obj for obj in objs if type(obj) ==
types.TupleType])

Alternative: initialize six counters to 0.  Scan list once and update
appropriate counter.

> Now, obviously I can (and will, now that 2.3 is officially released
:))
> replace the list comprehensions with itertools.ifilter, but I need
an
> itertools.ilen to find the length of such iterators.

You mean the associated sequence.

> I can imagine such a need arises in more useful situations than
this, but
> this is the particular case that brought the need to mind.
>
> The Python code is simple, obviously:
>
> def ilen(iterator):
>     i = 0
>     for _ in iterator:
>         i += 1
>     return i
>
> But it's a pity to use itertools' super-fast iterators and have to
use slow,
> raw Python to determine their length :)

If you mean a c-coded counter (which would not be an iterator itself)
equivalent to the above, that could be done.  Perhaps len() could be
upgraded/extended to accept an iterator and count when it can't get a
__len__ method to call.  The main downside is that iterators are
sometimes destructive (run once only).

In the meanwhile, is this really a bottleneck for you? or merely the
'pity' of a program running in 1 sec when 0.1 is possible?

Terry J. Reedy






More information about the Python-list mailing list