Update Counter collection in python with string, not letter
You can update it with a dictionary, since add another string is same as update the key with count +1:
from collections import Counterc = Counter(['black','blue'])c.update({"red": 1}) c# Counter({'black': 1, 'blue': 1, 'red': 1})
If the key already exists, the count will increase by one:
c.update({"red": 1})c# Counter({'black': 1, 'blue': 1, 'red': 2})
c.update(['red'])>>> cCounter({'black': 1, 'blue': 1, 'red': 1})
Source can be an iterable, a dictionary, or another Counter instance.
Although a string is an iterable, the result is not what you expected. First convert it to a list, tuple, etc.
You can use:
c["red"]+=1# orc.update({"red": 1})# or c.update(["red"])
All these options will work regardless of the key being present or not. And if present, they will increase the count by 1