Python collections Counter() Method with Examples

Counter():

The Counter class is a subset of the object data-set offered by Python3’s collections module. The Collections module exposes specialized container datatypes to the user, serving as an alternative to Python’s general-purpose built-ins such as dictionaries, lists, and tuples.

The counter is a subclass that counts hashable objects. When called, it constructs an iterable hash table implicitly.

elements() is a function of the Counter class that, when called on a Counter object, returns an itertool of all the known elements in the Counter object.

Parameters:

 It does not accept any parameters.

Return type:

 Returns an itertool for every element in the Counter object with a positive count.

Exceptions and Errors:

Because it returns an itertool rather than a specific data-container, it will output a garbage value when directly printed.
-> If an item's count has already been established in the Counter object, it will ignore items with zero and negative values.

Examples:

Example1:

Input:

Given String = "python-programs"

Output:

p  =  2
y  =  1
t  =  1
h  =  1
o  =  2
n  =  1
-  =  1
r  =  2
g  =  1
a  =  1
m  =  1
s  =  1

Example2:

Input:

Given List= [3, 1, 3, 4, 5, 6, 2, 2, 1, 5, 6, 9]

Output:

3  =  2
1  =  2
4  =  1
5  =  2
6  =  2
2  =  2
9  =  1

Collections Counter() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

i)elements() for string

Approach:

  • Import the Counter function from collections using the import keyword.
  • Give the string as static input and store it in a variable.
  • Apply the Counter() function for the string and store it in a variable(It returns a dictionary).
  • Apply elements() function for the Above result Counter Dictionary elements and print it.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable.
gvnstrng = "python-programs"
# Apply the Counter() function for the string and store it in a
# variable(It returns a dictionary).
rslt = Counter(gvnstrng)
# Apply elements() function for the Above result Counter Dictionary elements and print it.
print(rslt.elements())

Output:

<itertools.chain object at 0x7f8599afb780>

ii)Printing elements of Counter(String)

Approach:

  • Import the Counter function from collections using the import keyword.
  • Give the string as static input and store it in a variable.
  • Apply the Counter() function for the string and store it in a variable(It returns a dictionary).
  • Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.
  • Print the elements.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable.
gvnstrng = "python-programs"
# Apply the Counter() function for the string and store it in a
# variable(It returns a dictionary).
rslt = Counter(gvnstrng)
# Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.
for element in rslt.elements():
    # Print the elements.
    print(element)

Output:

p
p
y
t
h
o
o
n
-
r
r
g
a
m
s

iii)Printing elements with corresponding Frequency using elements()(String)

Approach:

  • Import the Counter function from collections using the import keyword.
  • Give the string as static input and store it in a variable.
  • Apply the Counter() function for the string and store it in a variable(It returns a dictionary).
  • Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.
  • Print the elements and their frequency using the [] operator.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable.
gvnstrng = "python-programs"
# Apply the Counter() function for the string and store it in a
# variable(It returns a dictionary).
rslt = Counter(gvnstrng)
# Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.
for element in rslt.elements():
    # Print the elements and their frequency using the [] operator.
    print(element, ' = ', rslt[element])

Output:

p  =  2
p  =  2
y  =  1
t  =  1
h  =  1
o  =  2
o  =  2
n  =  1
-  =  1
r  =  2
r  =  2
g  =  1
a  =  1
m  =  1
s  =  1

iv)Printing elements with corresponding Frequency(List)

Approach:

  • Import the Counter function from collections using the import keyword.
  • Give the list as static input and store it in a variable.
  • Apply the Counter() function for the list and store it in a variable(It returns a dictionary).
  • Loop in the elements of the Above result Counter Dictionary elements using for loop .
  • Print the elements and their frequency using the [] operator.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from collections using the import keyword.
from collections import Counter
# Give the list as static input and store it in a variable.
gvllst = [3, 1, 3, 4, 5, 6, 2, 2, 1, 5, 6, 9]
# Apply the Counter() function for the list and store it in a
# variable(It returns a dictionary).
rslt = Counter(gvllst)
# Loop in the Above result Counter Dictionary elements using for loop.
for element in rslt:
    # Print the elements and their frequency using the [] operator.
    print(element, ' = ', rslt[element])

Output:

3  =  2
1  =  2
4  =  1
5  =  2
6  =  2
2  =  2
9  =  1

v)Printing elements with corresponding frequency(String)

Approach:

  • Import the Counter function from collections using the import keyword.
  • Give the string as static input and store it in a variable.
  • Apply the Counter() function for the string and store it in a variable(It returns a dictionary).
  • Loop in the elements of the Above result Counter Dictionary elements using for loop.
  • Print the elements and their frequency using the [] operator.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from collections using the import keyword.
from collections import Counter
# Give the string as static input and store it in a variable.
gvnstrng = "python-programs"
# Apply the Counter() function for the string and store it in a
# variable(It returns a dictionary).
rslt = Counter(gvnstrng)
# Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.
for element in rslt:
    # Print the elements and their frequency using the [] operator.
    print(element, ' = ', rslt[element])

Output:

p  =  2
y  =  1
t  =  1
h  =  1
o  =  2
n  =  1
-  =  1
r  =  2
g  =  1
a  =  1
m  =  1
s  =  1

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import the Counter function from collections using the import keyword.
  • Give the list as user input using map(),list(),input(),int() functions and store it in a variable.
  • Apply the Counter() function for the list and store it in a variable(It returns a dictionary).
  • Loop in the elements of the Above result Counter Dictionary elements using for loop .
  • Print the elements and their frequency using the [] operator.
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from collections using the import keyword.
from collections import Counter
# Give the list as user input using map(),list(),input(),int() functions and store it in a variable.
gvllst = list(map(int, input('Enter some random elements = ').split()))
# Apply the Counter() function for the list and store it in a
# variable(It returns a dictionary).
rslt=Counter(gvllst)
# Loop in the Above result Counter Dictionary elements using for loop and elements() function.
for element in rslt:
    # Print the elements and their frequency using the [] operator.
    print(element, ' = ', rslt[element])

Output:

Enter some random elements = 7 1 2 1 8 9 2 4 6
7 = 1
1 = 2
2 = 2
8 = 1
9 = 1
4 = 1
6 = 1