Search code examples
pythoncollectionscounterattributeerror

Python collections.Counter: "AttributeError: 'Counter' object has no attribute 'total'"


As the title says, I'm getting

>> Counter({'red': 2, 'blue': 1}).total()
AttributeError: 'Counter' object has no attribute 'total'

even though it definitely does? Really at a loss here, thanks in advance!

I tried running it in another compiler, and that worked.


Solution

  • As Terry Spotts pointed out, this method is only available in Python version >=3.10.

    Easy workaround:

    sum(Counter_object.values())

    as mentioned here.