
Python Counter elements - Get all the items from the Counter object
The elements()
of a Counter object returns what can make all the items of it.
from collections import Counter
c = Counter(pen=3, book=2)
c1 = c.elements()
c2 = list(c1)
print(c1) # <itertools.chain object at 0x10dba6310>
print(c2) # ['pen', 'pen', 'pen', 'book', 'book']
c1
is an iterable so can be iterated in for statement.
from collections import Counter
c = Counter(pen=3, book=2)
c1 = c.elements()
for i in c1:
print(i)
# pen
# pen
# pen
# book
# book
Comments
Powered by Markdown