[issue45936] collections.Counter drops key if value is 0 and updating using += operator

Dennis Sweeney report at bugs.python.org
Mon Nov 29 22:29:37 EST 2021


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

This is consistent with the docstrings of the methods:

---------------------------------------------------------------------
>>> help(Counter.__iadd__)
Help on function __iadd__ in module collections:

__iadd__(self, other)
    Inplace add from another counter, keeping only positive counts.
    
    >>> c = Counter('abbb')
    >>> c += Counter('bcc')
    >>> c
    Counter({'b': 4, 'c': 2, 'a': 1})

>>> help(Counter.update)
Help on function update in module collections:

update(self, iterable=None, /, **kwds)
    Like dict.update() but add counts instead of replacing them.
    
    Source can be an iterable, a dictionary, or another Counter instance.
    
    >>> c = Counter('which')
    >>> c.update('witch')           # add elements from another iterable
    >>> d = Counter('watch')
    >>> c.update(d)                 # add elements from another counter
    >>> c['h']                      # four 'h' in which, witch, and watch
    4
---------------------------------------------------------------------


However, it seems Counter.__iadd__ is not mentioned at all at https://docs.python.org/3/library/collections.html#collections.Counter. I think there could be a doc update.

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45936>
_______________________________________


More information about the Python-bugs-list mailing list