The counter is a sub-class available inside the dictionary class. Using the Python Counter tool, you can count the key-value pairs in an object, also called a hash table object.

Why use Python Counter?

Here, are major reasons for using Python 3 Counter:

The Counter holds the data in an unordered collection, just like hashtable objects. The elements here represent the keys and the count as values. It allows you to count the items in an iterable list. Arithmetic operations like addition, subtraction, intersection, and union can be easily performed on a Counter. A Counter can also count elements from another counter

In this Python tutorial you will learn:

What is Python Counter?
Why use Python Counter?
Introduction to Python Counter
Counter with String
Counter with List
Counter with Dictionary
Counter with Tuple
Accessing, Initializing and Updating Counters
Deleting an Element from Counter
Arithmetic operation on Python Counter
Methods Available on Python Counter
Reassigning Counts in Python
Get and set the count of Elements using Counter

Introduction to Python Counter

Python Counter takes in input a list, tuple, dictionary, string, which are all iterable objects, and it will give you output that will have the count of each element. Syntax: Consider you have a following list : The list has elements x , y and z.When you use Counter on this list , it will count how many times x , y and z is present. The output if counter is used on list1 should be something like : So we have the count of x as 4, y as 2 and z as 2. To make use of Counter we need to import it first as shown in the below given example: Here is a simple example , that shows the working of Counter module. Output:

Counter with String

In Python, everything is an object and string is an object too. Python string can be created simply by enclosing characters in the double quote. Python does not support a character type. These are treated as strings of length one, also considered as a substring. In the example below, a string is passed to Counter. It returns dictionary format, with key/value pair where the key is the element and value is the count. It also considers space as an element and gives the count of spaces in the string. Example: Output:

Counter with List

A list is an iterable object that has its elements inside square brackets. The elements in the list when given to the Counter will be converted to a hashtable objects wherein the elements will become keys and the values will be the count of the elements from the list given. For example [‘x’,’y’,’z’,’x’,’x’,’x’,’y’,’z’]. Once you give the list the Counter, it will give you the count of each element in the list. Output:

Counter with Dictionary

A dictionary has elements as key/value pair, and they are written inside curly brackets. Once the dictionary is given to the Counter, it will be converted to a hashtable objects wherein the elements will become keys, and the values will be the count of the elements from the dictionary given. For example : {‘x’: 4, ‘y’: 2, ‘z’: 2, ‘z’: 2}. The Counter function will try to find the count of each of the key in the given dictionary. Output:

Counter with Tuple

Tuple is a collection of objects separated by commas inside round brackets. Counter will give you the count of each of the elements in the tuple given. Once the tuple is given to the Counter, it will be converted to a hashtable object wherein the elements will become keys and the values will be the count of the elements from the tuple given. Output:

Accessing, Initializing and Updating Counters

Initializing Counter

A Counter can be initialized by passing string value, list, dictionary, or tuple as shown below: You can also initialize a empty Counter as shown below:

Updating Counter

You can add values to the Counter by using update() method. The final code is : The output is:

Accessing Counter

To get the values from the Counter, you can do as follows: Output:

Deleting an Element from Counter

To delete an element from Counter you can make use of del , as shown in the example below: Example: Output:

Arithmetic operation on Python Counter

Arithmetic operation like addition, subtraction, intersection and union can be done on a Counter as shown in the example below: Example: Output:

Methods Available on Python Counter

There are some important methods available with Counter, here is the list of same:

elements() : This method will return you all the elements with count >0. Elements with 0 or -1 count will not be returned. most_common(value): This method will return you the most common elements from Counter list. subtract(): This method is used to deduct the elements from another Counter. update(): This method is used to update the elements from another Counter.

Example : elements()

Output:

Example: most_common(value)

Output:

Example:subtract()

Output:

Example:update()

Output:

Reassigning Counts in Python

You can re-assign counts of Counter as shown below: Consider you have a dictionary as : {‘x’: 5, ‘y’: 12, ‘z’: -2, ‘x1’:0} You can change the count of the element as shown below: Output: After executing you will see that y count is changed from 12 to 20

Get and set the count of Elements using Counter

To get the count of an element using Counter you can do as follows: Output: To set the count of the element you can do as follows: Output:

Summary:

Counter is a container that will hold the count of each of the elements present in the container. Counter is a sub-class available inside the dictionary class. Using the Python Counter tool, you can count the key-value pairs in an object, also called a hashtable object. The Counter holds the data in an unordered collection, just like hashtable objects. The elements here represent the keys and the count as values. It allows you to count the items in an iterable list. Arithmetic operations like addition, subtraction, intersection, and union can be easily performed on a Counter. A Counter can also count elements from another counter. The important methods available on a Counter are elements() , most_common(value), subtract() and update(). A counter can be used on a string, list, dictionary, and tuple.