Counter in Python

In this tutorial, you are going to learn about the counter in Python. It is a class of collection module of Python. Collection module is a special container that contains datatypes providing alternatives to Python’s general-purpose built-in containers dictionary, list, set, and tuple.

Different functions in counter() in Python

  1.  Counter():

    In this function, you can pass the list, tuple, dictionary, etc as the argument of the function. It returns a dictionary which is of a type of collections. Counter and the keys of dictionary represents the element, whereas the values represent the count of that element, i.e., how many times it occurs when it is passed as the argument of the function.

    from collections import Counter 
    # tuple 
    t=('a','b','a','c',1,1,1,'a') 
    x=Counter(t) 
    print(x) 
    # list 
    l=['a','b','a','c',1,1,1,'a'] 
    y= Counter(l) print(y) 
    # dictionary
    d={'a':1,'b':2}
    z=Counter(d) 
    print(z)
    print(type(z))

    Output:-

    Counter({'a': 3, 1: 3, 'b': 1, 'c': 1})
    Counter({'a': 3, 1: 3, 'b': 1, 'c': 1})
    Counter({'b': 2, 'a': 1})
    <class 'collections.Counter'>
  2. most_common():

    It returns a list which has the count of each element that how many times it occurs in the counter.
    For example: z.most_common(1)
    returns the [(‘b’,2)] where ‘b’ is the element and 2 is the count that how many times it occurs. It will not count ‘a’ as it does not have count greater than 1.

    # import the module
    from collections import Counter
    t=('a','b','a','c',1,1,1,'a')
    # Counter_1
    x=Counter(t)
    # most_common() without any argument
    print(x.most_common())
    
    # Counter_2
    d={'a':1,'b':2}
    z=Counter(d)
    # most_common() with an argument
    print(z.most_common(1))
    

    Output:-

    [('a', 3), (1, 3), ('b', 1), ('c', 1)]
    [('b', 2)]
  3. clear():

    It will clear or empty the counter.

    d={'a':1,'b':2}
    # Counter 
    z=Counter(d) 
    print(z)
    # use of clear() 
    z.clear() 
    print(z)

    Output:-

    Counter({'b': 2, 'a': 1})
    Counter()
  4. Update():

    It will update the counter.
    For ex: x.update(z)
    returns ({‘a’: 4, ‘b’: 3, 1: 3, ‘c’: 1}) where ‘a’ is the element and its count increase by 1 as z has a:1 and it gets updated in x.

    from collections import Counter
    # Counter_1 
    t=('a','b','a','c',1,1,1,'a') 
    x=Counter(t)
    # Counter_2 
    d={'a':1,'b':2} 
    z=Counter(d) 
    # use of update() 
    x.update(z) 
    print(x)

    Output:-

    Counter({'a': 4, 'b': 3, 1: 3, 'c': 1})
  5. & (add):

    It returns the intersecting or the common elements between the counters.

    from collections import Counter
    # Counter_1
    t=('a','b','a','c',1,1,1,'a')
    x=Counter(t)
    # Counter_2
    d={'a':1,'b':2}
    z=Counter(d)
    # use of &
    print(x & z)
    

    Output:-

    Counter({'a': 1, 'b': 1})
  6. | (ior):

    It returns the maximum count of the element from either counter.
    For example: x|a
    return ({‘a’: 3, 1: 3, ‘b’: 2}) where ‘a’ has counted 3 which comes from t as it has more number of a’s than d.

    from collections import Counter
    # Counter_1
    t=('a','b','a',1,1,1,'a')
    x=Counter(t)
    # Counter_2
    d={'a':1,'b':2,'c':0}
    z=Counter(d)
    # use of |
    print(x | z)
    

    Output:-

    Counter({'a': 3, 1: 3, 'b': 2})

    Note:- If some element has value 0 or negative, then it is not shown in the output. For example: In dictionary d it has ‘c’:0 which is not shown in the output when we print the ior between the x and z. It will only be printed when you print the counter, i.e., print(d) in the above program.

  7. – (isub):

    It subtracts the element that is common and returns only the element that has positive count.
    For example x – a
    return ({1: 3, ‘a’: 2}) as ‘c’ is present in d but not in x. So, it will not come in output. The element ‘b’ will have a negative count as 1-2 so, it will not come from the output.

    from collections import Counter 
    # Counter_1 
    t=('a','b','a',1,1,1,'a') 
    x=Counter(t) 
    # Counter_2 
    d={'a':1,'b':2,'c':1} 
    z=Counter(d) 
    # use of - 
    print(x - z)

    Output:-

    Counter({1: 3, 'a': 2})
    from collections import Counter
    c = Counter('which') 
    c.subtract('witch') 
    print(c)
    c.subtract(Counter('watch')) 
    print(c['h']) 
    print(c['w'])

    Output:-

    Counter({'h': 1, 'w': 0, 'i': 0, 'c': 0, 't': -1})
    0
    -1

Check out the other tutorials on python:
Python program to print numeric pattern
Python program to create a simple chatbox

Leave a Reply

Your email address will not be published. Required fields are marked *