update method of counter class

update() method takes an iterable object or a mapping object as parameter and adds the counts of elements to the counts of elements in the counter object.

Counter Updated 23 Mar 2017 1 min read

Method Name

update

Method Signature

update(itearableObjectOrMappingObject)

Method Overview

  • update() is like the add() counterpart of the subtract method.

  • update() takes a python iterable object or a mapping object and adds the counts of elements present in the iterable/mapping object to the counts of elements present in the counter object.

Example

python
import collections

counterObject = collections.Counter({"z":9,"y":8,"x":7})

print("Counter object before update:")

print(counterObject)

counterObject.update({"z":1,"y":2})

print("Counter object after update:")

print(counterObject)

Output

Output
Counter object before update:

Counter({'z': 9, 'y': 8, 'x': 7})

Counter object after update:

Counter({'z': 10, 'y': 10, 'x': 7})