Python Practice › Collections Programs › Program 17
Expand a Counter
Write a Python program to list every item of a Counter as many times as its count.
Output
['a', 'a', 'b', 'b', 'b', 'c']
Approach
elements().
Solution
from collections import Counterprint(sorted(Counter(a=2, b=3, c=1).elements()))Output
['a', 'a', 'b', 'b', 'b', 'c']
Learn the concept
New to this topic? Read the lesson, then come back.