I'm writing something in Python that involves using the Counter
class in exactly the way the name implies: counting items one by one. Every time I want to count something I make a single element list out of it to feed to Counter.update
:
from collections import Counter
c = Counter()
c.update(["foo"]) # Works, but do I really need to create a list every time I want to do this?
Is this the right way to do it? Creating the list feels awkward. I've been looking for something like this:
c.add("bar") # Method doesn't exist
Creating my own Counter subclass seems pretty painless, but it's nicer to deal with the built-in classes as designed if possible, and I wonder if I'm missing something.
class MyCounter(Counter):
def add(self, item):
self.update([item])
c2 = MyCounter()
c2.add("bar") # Now it works