w3resource

Python Counter Exercises & Solutions


Python counter Data Type [ 10 exercises with solution ]


[An editor is available at the bottom of the page to write and execute the scripts.   Go to the editor]

A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

1. Write a Python program to create a 'Counter' of the letters in the string "Python Exercise!".
Click me to see the sample solution

2. Write a Python program that creates a 'Counter' from a list of elements and print the most common elements along with their counts.
Click me to see the sample solution

3. Write a Python program that creates a counter of the vowels in the word "Python Exercises".
Click me to see the sample solution

4. Write a Python program that creates two 'Counter' objects and finds their union, intersection, and difference.
Click me to see the sample solution

5. Write a Python program that creates a counter of the words in a sentence and prints the words in ascending and descending order of their frequency.
Click me to see the sample solution

6. Write a function that takes two 'Counter' objects and returns their sum.
Click me to see the sample solution

7. Write a Python program that creates a 'Counter' for a list of items and converts it to a list of unique items with their counts.
Click me to see the sample solution

8. Write a Python program that creates a 'Counter' for a list of words and removes all items with a count less than a certain value..
Click me to see the sample solution

9. Write a python program to find the Jaccard similarity coefficient between two lists using 'Counter' objects.
Click me to see the sample solution

10. Write a Python program that creates a 'Counter' for a list of items and uses dictionary-style access to update the count of specific items.
Click me to see the sample solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.