Counter class is a special type of object data-set provided with the
collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Python’s general purpose built-ins like dictionaries, lists and tuples.
Counter is a sub-class which is used to count hashable objects. It implicitly creates a hash table of an iterable when invoked.
elements() is one of the functions of Counter class, when invoked on the Counter object will return an itertool of all the known elements in the Counter object.
Parameters : Doesn’t take any parameters
Return type : Returns an itertool for all the elements with positive count in the Counter object
Errors and Exceptions :
-> It will print garbage value when directly printed because it returns an itertool, not a specific data-container.
-> If the count of an item is already initialized in Counter object, then it will ignore the ones with zero and negative values.
Code #1: Working of elements() on a simple data container
g g e e e e k k s s f o r
Code #2: Elements on a variety of Counter Objects with different data-containers
o k k f r e e e e s s g g for python python python geeks geeks geeks geeks gfg gfg 1 2 2 19 21 21 44 15 12 13 5 5 5 a a e e e e e b b b c c c c c c d
Code #3: To demonstrate what elements() return when it is printed directly
itertools.chain object at 0x037209F0
Code #4: When the count of an item in Counter is intialised with negative values or zero.
a : 2 a : 2 x : 3 x : 3 x : 3 b : 3 b : 3 b : 3 z : 1 y : 5 y : 5 y : 5 y : 5 y : 5
Note: We can infer from the output that items with values less than 1 are omitted by elements().
Counter object along with its functions are used collectively for processing huge amounts of data. We can see that Counter() creates a hash-map for the data container invoked with it which is very useful than by manual processing of elements. It is one of a very high processing and functioning tools and can even function with a wide range of data too.
- Python | Minimum number of subsets with distinct elements using Counter
- Python Counter | Majority Element
- Python dictionary, set and counter to check if frequencies can become same
- Anagram checking in Python using collections.Counter()
- Dictionary and counter in Python to find winner of election
- Python Counter| Find all duplicate characters in string
- Python Counter| Find duplicate rows in a binary matrix
- Python Counter to find the size of largest subset of anagram words
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Reading Python File-Like Objects from C | Python
- Timer Objects in Python
- File Objects in Python
- Barrier Objects in Python
- Mutable vs Immutable Objects in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.