Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 10, 2020
Embed
What would you like to do?
from collections import Counter
c=Counter({'a':2,'b':1,'c':4,'d':3})
print (c)#Output:Counter({'c': 4, 'd': 3, 'a': 2, 'b': 1})
elem=c.elements()
#returns an iterator
print (elem)#Output:<itertools.chain object at 0x0240EC10>
#converting iterator to list object
print (list(elem))#Output:['a', 'a', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd']
c1=Counter({'a':0,'b':2})
#Elements with count less than one are ignored.
print (list(c1.elements()))#Output:['b', 'b']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment