Expand a Counter

EasyCollections Programs · 17 of 20

Write a Python program to list every item of a Counter as many times as its count.

Advertisement
Output
['a', 'a', 'b', 'b', 'b', 'c']

Approach

  1. elements().

Solution

Python
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.

More collections programs

← All 20 Collections Programs

Leave a Comment