PyDocs

Nav

Data Types

Counter.elements

  • Counter.elements()
Iterator over elements repeating each as many times as its count.

>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']

Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1

>>> import math
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> math.prod(prime_factors.elements())
1836

Note, if an element's count has been set to zero or is a negative
number, elements() will ignore it.
1 Example
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> sorted(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']
from the Python docs
See Also
No see-alsos for collections/Counter.elements
0 Notes
No notes for Counter.elements