Python | Counter Objects | elements()

08 Aug, 2021
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 that 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 parametersReturn type : Returns an itertool for all the elements with positive count in the Counter objectErrors 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  
Python3
# import counter class from collections modulefrom collections import Counter # Creation of a Counter Class object using# string as an iterable data containerx = Counter("geeksforgeeks") # printing the elements of counter objectfor i in x.elements():    print ( i, end = " ")
Output: 
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  
Python3
# import counter class from collections modulefrom collections import Counter # Creation of a Counter Class object using# a string as an iterable data container# Example - 1a = Counter("geeksforgeeks") # Elements of counter objectfor i in a.elements():    print ( i, end = " ")print()     # Example - 2b = Counter({'geeks' : 4, 'for' : 1,            'gfg' : 2, 'python' : 3}) for i in b.elements():    print ( i, end = " ")print() # Example - 3c = Counter([1, 2, 21, 12, 2, 44, 5,              13, 15, 5, 19, 21, 5]) for i in c.elements():    print ( i, end = " ")print()                            # Example - 4d = Counter( a = 2, b = 3, c = 6, d = 1, e = 5) for i in d.elements():    print ( i, end = " ")
Output:
g g e e e e k k s s f o r 
geeks geeks geeks geeks for gfg gfg python python python 
1 2 2 21 21 12 44 5 5 5 13 15 19 
a a b b b c c c c c c d e e e e e 
Code #3: To demonstrate what elements() return when it is printed directly  
Python3
# import Counter from collectionsfrom collections import Counter # creating a raw data-setx = Counter ("geeksforgeeks") # will return a itertools chain object# which is basically a pseudo iterable container whose# elements can be used when called with a iterable toolprint(x.elements())
Output: 
itertools.chain object at 0x037209F0
Code #4: When the count of an item in Counter is initialized with negative values or zero.  
Python3
# import Counter from collectionsfrom collections import Counter # creating a raw data-set using keyword argumentsx = Counter (a = 2, x = 3, b = 3, z = 1, y = 5, c = 0, d = -3) # printing out the elementsfor i in x.elements():    print( "% s : % s" % (i, x[i]), end ="\n")
Output: 
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().
Applications: 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. 
gabaa406
Mitrajit
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Python | os.path.join() method
Introduction To PYTHON