Counting number of each item in a list.

Paul Rubin http
Sun Mar 19 13:50:02 CET 2006

"sophie_newbie" <paulgeeleher at> writes:
> I suppose that the easiest way to do that is to convert it to a 2
> dimensional array? Is there any easy way?

Use a hash table.  Untested:

   h = 0
   for x in your_list:
      h[x] = h.get(x, 0) + 1

You can then use something like sorted(h.items()) to get a sorted list
of those counts.

More information about the Python-list mailing list