I want to know how many items are in a Python Counter, including the duplicates. I tried len
and it tells me the number of unique items:
>>> c = Counter(x=3,y=7)
>>> len(c)
2
The best I have is sum(c.itervalues())
which I suppose isn't terrible, but I was hoping the Counter object caches the value so I could access it in O(1).
The Counter docs give your sum(c.itervalues())
answer as the standard pattern for this in the "Common patterns for working with Counter objects" section, so I doubt there's anything better.
As with the other iter*
methods on dictionaries, in Python 3 itervalues
is replaced by values
.