[Python-Dev] Re: Candidate Itertools

Raymond Hettinger python at rcn.com
Tue Jun 15 15:07:40 EDT 2004

[David Eppstein]
> I'd probably use count_elements, but as Bob I. already said, I'd
> the dict to the transposed pairs of its items.

An alternate dictionary constructor would provide that flexibility:

  def countkeys(cls, iterable):
      b = cls()
      for elem in iterable:
          [elem] = b.get(elem, 0) + 1
      return b

However, the added flexibility makes the use case more complicated:

  >>> from heapq import nlargest
  >>> words = (word  for line in myfile  for word in line.split())
  >>> nlargest(3, pairwise(dict.countkeys(words).iteritems()))
  [(200, 'super'), (129, 'hero'), (103, 'villain')]

That last line is much more involved than writing:

  >>> nlargest(3, count_elements(words))

I find the latter to be more readable and to be a more obvious tool for
working with nlargest(), nsmallest(), sorted(), min(), and max().  Also,
it does less work under the hood because it doesn't have to build two
different tuples for each entry.

OTOH, if you actually want to end-up with a dictionary, it is somewhat
wasteful to build one, throw it away, and then rebuild it from the

  >>> dict(count_elements(words))


More information about the Python-Dev mailing list