Returning histogram-like data for items in a list

David Isaac aisaac0 at verizon.net
Fri Jul 22 00:14:10 EDT 2005


"Ric Deez" <deez at next-level.com.au> wrote in message
news:dbpat7$28o$1 at nnrp.waia.asn.au...
> I have a list:
> L1 = [1,1,1,2,2,3]
> How can I easily turn this into a list of tuples where the first element
> is the list element and the second is the number of times it occurs in
> the list (I think that this is referred to as a histogram):

For ease of reading (but not efficiency) I like:
hist = [(x,L1.count(x)) for x in set(L1)]
See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277600

Alan Isaac





More information about the Python-list mailing list