[Python-Dev] function for counting items in a sequence

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Apr 8 03:29:35 CEST 2007


Steven Bethard wrote:
> Guido commented in the tracker that it would be worth discussing
> whether that last item (``item_counts['d']``) should return 0 (as a
> defaultdict would) or raise KeyError (as a dict would).

If you've asked for a count of 'd's, and there aren't
any 'd's, the most sensible answer would seem to be
0, not an exception.

Also it would be a useful property if you were
guaranteed that all items in the input sequence
are usable as keys in the result without causing
an exception.

--
Greg


More information about the Python-Dev mailing list