DEV Community

Shyam Salil
Shyam Salil

Posted on


Hello fellow developers,

Taking this opportunity to let you all know that I have officially released a Python package - py_counter!

What it does? : This package takes in a list or a tuple sequence as an argument and returns a hash map of each element with the count of its occurrences in the sequence as a key:value pair. Useful right?

When I was solving coding problems like finding duplicates, finding number of duplicates, finding the recurrent characters, etc, I found myself looking for a library/package that calculated the occurrence of element in a list/tuple and returned it, thus making it easier for us.
But I wasn't convinced with what I found.

But then, isn't that our responsibility & power as a developer/programmer?

When we look for something that we need, to solve for a problem and we realize that it's not already available, WE CREATE IT.

We create a solution & we make it available to everybody who might face the same problem.

The joy of open source!

With that said, here are the details regarding installation, usage and code snippets to get started.

Link to the PyPi package

How to install?

Simply use the pip install to install the package.

pip install py_counter


python -m pip install py_counter

How to import and use?

The first step is to import inside your code:

import py_counter

The next step is to instantiate the class

word = py_counter.py_counter() #here word is the variable object

Then we simply use it :)

print(word.counter([1,2,3,4,1,3,2])) #here counter() is the method/function
OUTPUT: {1: 2, 2: 2, 3: 2, 4: 1}

Some more examples below:

Output: {'shyam': 1}

Output: {'i': 2, 'am': 2, 'a': 1, 'developer': 1}

Output: {'c': 1, 'o': 1, 'd': 1, 'i': 3, 'n': 2, 'g': 2, 's': 1, 'a': 2, 'm': 1, 'z': 1}

Output: {'codingisamazing': 2}

arr = ["code","is","code"]
Output: {'code': 2, 'is': 1}
    Points to note:
  • If you assign a list or a tuple to a variable and reference the variable while calling counter(), then no pointers on it! Good to go. Because the counter method takes in one argument only.
  • So if you don't want to assign a variable, but want to check, you can print it by print(variable.counter("amazing")) or, print(variable.counter((1,2,3,4,2,1))), such that the counter function has only one argument.

With that said, signing off. Hope this is useful to someone :) Please let me know via email or comments for any issues encountered with the package or for any comments/feedback on this.

Thank you!

Top comments (0)