[Python-Dev] Candidate Itertools

Aahz aahz at pythoncraft.com
Mon Jun 21 23:52:55 EDT 2004

On Sat, Jun 19, 2004, Raymond Hettinger wrote:
> [Aahz]
>>> Feedback is requested for two prospective itertools:
>>> def count_elements(iterable):
>>>     b = {}
>>>     for elem in iterable:
>>>         b[elem] = b.get(elem, 0) + 1
>>>     return ((cnt, elem) for elem, cnt in b.iteritems())
>> +1 provided it returns either (elem, cnt) or the dict itself.
> Hmm, there seems to be 100% support for returning a dictionary and zero
> support for my iterable (cnt, elem) ... to feed min(), max(), sorted(),
> nlargest(), and nsmallest().

The problem is that it's useful for those operations -- and nothing
else.  It's narrow functionality, and anyone looking just at itertools
will find this confusing; it's just not the natural definition for
something named ``count_elements``, and I suspect it is not possible to
create a good name for your purpose.  Anyone who understands Python will
expect ``count_elements`` to return (elem,cnt) or a dict.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith, c.l.py

More information about the Python-Dev mailing list