Back
Close

Journey to Master Python (WIP)

ageekymonk
17.8K views

Collections module

Better way to count elements

Collections module provide a Counter class which can used to count elements in a list. It also provides utility functions like most_common for getting most common elements in the list.

counter class

defaultdict

defaultdict behaves like normal dict with one key difference. It has a factory function that takes no arguments which provides values for non existent key

defaultdict class

One most popular example is one liner tree function. You can create a nested dict without even initialising its parents.

defaultdict class

namedtuple

namedtuple is a factory function to create tuple like object but can access its elements with attribute name.

namedtuple
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants