Open this example in the interactive library
Source code
from collections import Counter
stock = Counter(apple=5, pear=3)
print(dict(stock - Counter(apple=2)))Sample output
{'apple': 3, 'pear': 3}
Subtract inventory counts. A self-contained example using the Python standard library.
Open this example in the interactive library
from collections import Counter
stock = Counter(apple=5, pear=3)
print(dict(stock - Counter(apple=2))){'apple': 3, 'pear': 3}