Journey to Master Python (WIP)


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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 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 is a factory function to create tuple like object but can access its elements with attribute name.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content