What is defaultdict and Counter?

Python lists, dictionaries and sets · Freshers and experienced

Model answer

Both are in collections. defaultdict creates a missing value automatically with a factory such as list or int, which is handy for grouping. Counter counts items: Counter(words).most_common(3). They reduce boilerplate compared to checking if a key exists.

Learn this topic

Dictionaries, Tuples and Sets in Python in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions