Returning histogram-like data for items in a list

George Sakkis gsakkis at rutgers.edu
Thu Jul 21 22:20:26 EDT 2005


"jeethu_rao" <jeethurao at gmail.com> wrote:

> Adding to George's reply, if you want slightly more performance, you
> can avoid the exception with something like
>
> def hist(seq):
>     h = {}
>     for i in seq:
> h[i] = h.get(i,0)+1
>     return h.items()
>
> Jeethu Rao

The performance penalty of the exception is imposed only the first time a distinct item is found. So
unless you have a huge list of distinct items, I seriously doubt that this is faster at any
measurable rate.

George





More information about the Python-list mailing list