Python Collections Module

A quick overview of Counter and ChainMap in Python Collections Module.

Photo by Ulises Baga on Unsplash

Python Collections Module:

Collections in Python are container used for storing data such as List,Dictionary,Set,Tuple.

Python’s Collection module implements specialized container data types.

  • ChainMap:
    A ChainMap groups multiple dictionary or other mappings together to create a single, updateable view. If no maps are specified, a single empty dictionary is provided so that a new chain always has at least one mapping
collections.ChainMap(*maps)

https://gist.github.com/IndhumathyChelliah/4dc1cf4044ab12f2d769d3c3acaba85b

All of the usual dictionary methods are supported for ChainMap object.In addition it has maps attribute.

Dictionary methods available in ChainMap object
  1. maps:
    maps attribute in ChainMap will return list of mappings.All list methods are supported in that.
list methods supported by maps

https://gist.github.com/IndhumathyChelliah/3b08ac03a207469ee515b31ff1192188

2. new_child(m=None)

Returns a new ChainMap containing a new map followed by all of the maps in the current instance.If m is specified, it becomes the new map at the front of the list of mappings; if not specified, an empty dict is used.

https://gist.github.com/IndhumathyChelliah/56b41e416e0174f11ee36d8fea7b6247

3.parents:

Returns a new ChainMap containing all of the maps in the current instance except the first one. This is useful for skipping the first map in the search.

https://gist.github.com/IndhumathyChelliah/b5f398a751e00f1a683c435a6f0758e1

The ChainMap class only makes updates (writes and deletions) to the first mapping in the chain while lookups will search the full chain.

https://gist.github.com/IndhumathyChelliah/85f1ad82782cbb01f16c98d0d44b6421

If we want to update values in other mapping also,it is easy to make a subclass that updates keys found deeper in the chain.

https://gist.github.com/IndhumathyChelliah/54528a0a43ccc2780c720055358dd686

  • Counter

A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts.

collections.Counter(iterable-or-mapping)

https://gist.github.com/IndhumathyChelliah/a8cffec8a56ac066ac41e0c7c5eeda28

Getting count of elements and deleting elements from Counter object:

https://gist.github.com/IndhumathyChelliah/9a851fcd3f26530d635efb02365726b2

methods supported by Counter object

Counter object supports these three methods beyond those available for dictionaries.

1.elements()
2.most_common(n)
3.subtract(iterable or mapping)
  1. elements()

Return an iterator over elements repeating each as many times as its count. Elements are returned in the order first encountered. If an element’s count is less than one, elements() will ignore it.

https://gist.github.com/IndhumathyChelliah/69d4b1d861801847a5cea5095ad083b6

2. most_common(n)

Return a list of n most common elements and their count from most common to the least.If n is omitted or None, it returns all elements in the Counter.Elements with equal count are returned in the order first encountered.

https://gist.github.com/IndhumathyChelliah/9c6ed0a0787aebb3a85c3cce39db6d57

3.subtract(iterable or mapping)

Elements are subtracted from an iterable or from another mapping (or counter). Both inputs and outputs may be zero or negative.

https://gist.github.com/IndhumathyChelliah/adae8851a277b082841c9565e01cbb83

All dictionary methods are supported by Counter objects.Below mentioned two methods behave differently for counter.

fromkeys(iterable)
update(iterable or mapping)

fromkeys(iterable)

This class method is not implemented for counter objects

update(iterable or mapping):

update method add count from other counter.In dict.update() method,it replaces them.Also, the iterable is expected to be a sequence of elements, not a sequence of (key, value) pairs.

https://gist.github.com/IndhumathyChelliah/9265721df5bd5c0427ad998e6dd50e16

Mathematical Operations in Counter object:

Addition and subtraction operation on counters is done by adding/subtracting the counts of corresponding elements.
Intersection and Union represents the minimum and maximum of corresponding counts.
Input can have signed counts,but output will exclude results with count of zero or less.

https://gist.github.com/IndhumathyChelliah/0750532f1c4a09bf50c6fab407c947f7https://gist.github.com/IndhumathyChelliah/7de439184fc2bcd7b8bf3538796b63fa

Unary addition and subtraction:

Unary Addition:Removes zero and negative values.

https://gist.github.com/IndhumathyChelliah/ce00e45053e1a077b0e2789d16d0d10c

Common patterns for working with Counter object:

list()– By using list(), we can convert Counter object to List which contains unique elements from Counter object.

set()-By using set(), we can convert Counter object to set containing elements from Counter object

dict()-Using dict(), we can convert Counter object to regular dictionary

items() method can be used to convert Counter object to List of tuples containing (element,count)pairs.

https://gist.github.com/IndhumathyChelliah/65184ccada9114159dfe043793c61a10

How to find the least common element and most common element:

most_common() method will return list of elements from most common to least common.

Most common element can be accessed from the first index of that list.
Least common element can be accessed from the last index of that list.Last element of the list can be accessed by index -1

https://gist.github.com/IndhumathyChelliah/33d0fd57fa501d4c353952f4e2f4d281

How to find the total of all counts:

Total of all counts is calculated by using sum(c.values())

from collections import Counter
d1=Counter({'a':-2,'b':5,'c':3,'d':4})
print (sum(d1.values()))#Output:10

Note:

  • ChainMap
maps()-Return Type is List
newchild(m=None)-Return Type is new ChainMap object
parents- Return type is new ChainMap object.
  • Counter:
element() -Return type is iterator
most_common(n)-Return Type is List
subtract(iterable or mapping)-Return Type is None.It will update the existing Counter object.
update()-Return Type is None.It will update the existing counter object

Difference between update() and subtract() method:

update() method is used to add counts from another counter.
subtract() method is used to subtract counts from another counter.

Difference between subtract() method and subtraction operation on two Counter objects:

subtract() method:
*By using subtract()method,we can subtract count of corresponding elements from  first counter object and second Counter object and update the result in first Counter object.Return Type is None
* Both input and output can contain result with counts negative or zero.
Subtraction Operation:
* Subtraction Operation is done by subtracting counts of corresponding elements from counter objects and return the result in new Counter object
* Input can contain signed counts but output will exclude results with counts of zero or less.

Difference between update() method and addition operation on two Counter objects:

update() method:
* By using we can update()method,we can add count of corresponding elements from  first counter object and second Counter object and update the result in first Counter object.Return Type is None
* Both input and output can contain result with counts negative values or zero.
Addition Operation:
* Addition Operation is done by adding counts of corresponding elements from counter objects and return the result in new Counter object
* Input can contain signed counts but output will exclude results with counts of zero or less.

Resources:

ChainMap:

https://docs.python.org/3.8/library/collections.html#chainmap-objects

Counter:

https://docs.python.org/3.8/library/collections.html#counter-objects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s