Python meets Marvel💥

Misleading title.
Today I want to talk about one of the most useful modules in Python: Collections.:zap:

Collections offers a set of special container data types which offer extended superpowers over the usual list, dict and tuples.

Let’s have a quick look.

:arrow_forward: ChainMap: is used to group multiple dictionaries together in a single, updateable view.

What this means is you can operate on by extracting or updating values using a single view on multiple dicts.

As ChainMap contains just the reference to original dicts, any updates to chainmap will reflect in original dict too.

:arrow_forward: Deque: is an extension of list but thinks itself as a Queue. Weird.

Deque (Double Ended Queue) has all methods of a list along with appending/deleting on either ends, as its name suggests.

You can also rotate clockwise or anti-clockwise with just a single command.

:arrow_forward: Counter: This is my favorite.

Counter, also a dict subclass, simply counts the number of hashable objects present and stores it in a dictionary.

You can quickly find the top n occurring values by calling most_common on it.

Apart from these, there are more containers such as NamedTuple, DefaultDict which offer extended methods over stock tuple and dicts.:bulb:

Have you used Collections?

#python #datascience