Working with Python Counter

Python Counter is a container that contains a counter for each of the items present in the container. Containers are objects that contain other objects and provide a way to iterate over them. Examples of built-in containers are Tuple, List, and Dictionary. The rest are included in the Collections module. The Collections module in Python3 provides the user with specialized container data types, providing an alternative to general-purpose Python built-in programs such as dictionaries, lists, and tuples. The counter is a dict subclass that is used to count hashable objects. It counts the number of occurrences of any value in the container. Click Execute to run the Python Counter Example online and result.
Working with Python Counter Execute
from collections import Counter

c = Counter("google")
      
print(c)
Updated: Viewed: 2479 times

How to initialize a Python counter?

To use Counter, you need to import it first:

Python Counter Syntax
from collections import Counter

How to use Python Counter?

A Counter is a dictionary where objects are stored as keys and are considered values. You can provide a sequence or iteration of hashable objects as an argument to the class constructor for counting counter. Counter internally iterates through the input sequence, counts how many times a given object is encountered, and stores the objects as keys and counters as values. If you want to count multiple objects simultaneously, you need to use sequence or iteration to initialize the counter.

Python Counter Example
from collections import Counter

c = Counter("hello")
      
print(c)
      
# output: Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})

Python Counter Examples

The following examples demonstrate how Counter works in Python with a detailed description:

Counter with Python String

In the example below, a string is passed to Counter. It returns a dictionary with key/value pairs, where the key is the element and the value is the count. Whitespace is also treated as an element by the Counter.

Python Counter with String Example
from collections import Counter

my_string = "Hello, World"

print(Counter(string))
      
# output: Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ',': 1, ' ': 1, 'W': 1, 'r': 1, 'd': 1})

Counter with Python List

In the example below, a list is passed to Counter. This Counter will convert the elements in the list passed to it into hash table objects where the elements will become keys, and the values will be the number of elements in the list. Once you pass the list to Counter, it will provide you with the count of each element in the list.

Python Counter with List Example
from collections import Counter

my_list = ['a','a','a','b','c','b','d']

print(Counter(my_list))
      
# output: Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1})

Counter with Python Dictionary

The following example passes a dictionary to Counter. Dictionaries contain pairs of keys and values. Once the dictionary has been passed to the Counter, the elements will become the keys and the values will be the number of elements in the dictionary.

Python Counter with Dictionary Example
from collections import Counter

my_dict =  {'a': 4, 'b': 2, 'c': 2, 'd': 2}

print(Counter(my_dict))
      
# output: Counter({'a': 4, 'b': 2, 'c': 2, 'd': 2})

Counter with Python Tuple

Below is an example of how a Tuple is passed to a Counter. A tuple is a collection of objects separated by commas. Once a tuple has been given to a counter, it will be converted to a hash table object where the elements will become the keys and the values the number of elements from the tuple.

Python Counter with Tuple Example
from collections import Counter

my_tuple = ('a','c','e','a','a','a','b','b')

print(Counter(my_tuple))
      
# output: Counter({'a': 4, 'b': 2, 'c': 1, 'e': 1})

Why use Python Counter?

The main reasons for using Python 3 Counter:

  • The counter stores data in an unordered collection like hash table objects. Items here represent keys and counter as values.
  • The counter can efficiently perform arithmetic operations such as addition, subtraction, intersection, and union.
  • The counter allows you to count the items in a repeating list.
  • The counter can also count items from another counter.

See also