Tutorial by Examples

Counter is a dict sub class that allows you to easily count objects. It has utility methods for working with the frequencies of the objects that you are counting. import collections counts = collections.Counter([1,2,3]) the above code creates an object, counts, which has the frequencies of all ...
collections.defaultdict(default_factory) returns a subclass of dict that has a default value for missing keys. The argument should be a function that returns the default value when called with no arguments. If there is nothing passed, it defaults to None. >>> state_capitals = collections.d...
The order of keys in Python dictionaries is arbitrary: they are not governed by the order in which you add them. For example: >>> d = {'foo': 5, 'bar': 6} >>> print(d) {'foo': 5, 'bar': 6} >>> d['baz'] = 7 >>> print(a) {'baz': 7, 'foo': 5, 'bar': 6} >&g...
Define a new type Person using namedtuple like this: Person = namedtuple('Person', ['age', 'height', 'name']) The second argument is the list of attributes that the tuple will have. You can list these attributes also as either space or comma separated string: Person = namedtuple('Person', 'age,...
Returns a new deque object initialized left-to-right (using append()) with data from iterable. If iterable is not specified, the new deque is empty. Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, me...
ChainMap is new in version 3.3 Returns a new ChainMap object given a number of maps. This object groups multiple dicts or other mappings together to create a single, updateable view. ChainMaps are useful managing nested contexts and overlays. An example in the python world is found in the implemen...

Page 1 of 1