Counters in Python | How to Initialize and UpdateIn this tutorial, we will discuss the Counter that included in the collection module. We will also explain how we can use it to solve problems. First, let's have a brief introduction of Counter. What is Counter?Counters are referred to as the container that holds the count of each of the elements that are available in the container. They allow us to access the stored object. The dictionary class holds the Counter as the sub-class. Counter helps us to count the key-value pairs in an object. They are also known as hash table objects. The list, tuple, dictionary are examples of the built-in container, and many are included in the collection module. It can be considered as a bag or multiset of other languages. Below is the syntax of the Counter. Syntax: InitializationWe can call the constructor of the counter can be called in any one of the following ways:
Why do we use Counter?Following are the main reasons for using Counter in the Python program.
Implementation of CountersPython takes iterable objects like list, dictionary, tuple, string as an argument and returns the count of each element. Consider the following list that contains several elements. List1 = [a, b, a, a, c, d, e, c, d, f] The list has a duplicate element. We will use the count values that show how many times these elements are present. Let's see the below example. Example - 1: Output: Counter({'a': 3, 'c': 2, 'd': 2, 'b': 1, 'e': 1, 'f': 1}) So we have the count of a as 3, b as 1, e as 1, c as 2, f as 1. Example - 2: Output: Counter({'X': 4, 'Y': 4, 'Z': 2}) Counter({'X': 5, 'Z': 4, 'Y': 2}) Counter({'B': 5, 'A': 3, 'C': 2}) Updating CounterThe collection module provides the update() method which allows us to update or add value. The syntax is following - Let's understand the following example. Example - Output: Counter({'o': 4, ' ': 3, 't': 3, 'a': 3, 'e': 2, 'l': 2, 'T': 2, 'i': 2, 'W': 1, 'c': 1, 'm': 1, 'J': 1, 'v': 1, 'p': 1, 'n': 1, 'u': 1, 'r': 1, 's': 1, '!': 1}) Accessing CounterWe can access the values from the Counter. Let's understand the following example. Example - Output: u : 2 T : 2 u : 1 t : 3 o : 4 r : 1 i : 2 a : 3 l : 2 s : 1 Deleting an Element from CounterThe del keyword is used to delete the element from the counter. Let's understand the following example. Example - Output: Dictionary After Deletion: Counter({'a': 3, 'b': 1}) How to Perform Arithmetic Operation on Python CounterAs we mentioned above, we can perform various arithmetic operations such as addition, subtraction, interaction and union. Let's understand the following example. Example - Output: Counter({'b': 11, 'c': 11, 'd': 7, 'a': 5}) Counter({'a1': 10, 'd': 7, 'a': 5}) Counter({'b': 1}) Counter({'c': 14, 'b': 10, 'd': 7, 'a': 5}) Counter with StringAs we know, everything in Python is an object, so the string is an object as well. Strings are enclosed character in characters in the double quotes. Python doesn't have a character type. In the following example, we will pass the string to the Counter, which will return the character dictionary with key/value pair. The key is the element, and value is the count. Example - Output: Counter({'o': 4, ' ': 3, 't': 3, 'a': 3, 'e': 2, 'l': 2, 'T': 2, 'i': 2, 'W': 1, 'c': 1, 'm': 1, 'J': 1, 'v': 1, 'p': 1, 'n': 1, 'u': 1, 'r': 1}) Counter with DictionaryA dictionary stores the elements in the key-value pair define inside the curly brackets. When we pass the dictionary into the Counter, it will return to a hashtable object element. The element will become keys, and the vale will be counted as the element from the dictionary given. Let's understand the following example. Example - Output: Counter({'b': 6, 'a': 5, 'c': 2}) Counter with TupleTuples are immutable data-structure where elements are stored separated by commas inside round brackets. A tuple is converted into a hashtable object. A counter will provide the count of each element in the tuple given. The element will become keys, and the vale will be counted as the element from the dictionary given. Let's understand the following example. Example - Output: Counter({'x': 4, 'y': 2, 'z': 2}) Some Important Methods in Python CounterThe following are some commonly used methods in Counter.
Example - Output: a a a a a b b
Example - Output: [('a', 5), ('b', 2)] [('a', 5), ('b', 2), ('d', 0), ('c', -2)]
Example - Output: Counter({'z': 7, 'x': 2, 'y': -2}) ConclusionIn this tutorial, we have covered almost every important concept of the Python Counter. The counter is a kind of container that stores the count of each of the elements available in the container. Counters allow us to count the items in an iterable list, and we can use it the other iterable. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India