PyDocs

Nav

Data Types

Counter.subtract

collections

method of Counter · Added in 3.2 (source)
  • Counter.subtract(**kwargs)
  • Counter.subtract(iterable, /, **kwargs)
  • Counter.subtract(mapping, /, **kwargs)
Like dict.update() but subtracts counts instead of replacing them.
Counts can be reduced below zero.  Both the inputs and outputs are
allowed to contain zero and negative counts.

Source can be an iterable, a dictionary, or another Counter instance.

>>> c = Counter('which')
>>> c.subtract('witch')             # subtract elements from another iterable
>>> c.subtract(Counter('watch'))    # subtract elements from another counter
>>> c['h']                          # 2 in which, minus 1 in witch, minus 1 in watch
0
>>> c['w']                          # 1 in which, minus 1 in witch, minus 1 in watch
-1
1 Example
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
>>> c
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
from the Python docs
See Also
No see-alsos for collections/Counter.subtract
0 Notes
No notes for Counter.subtract