Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python Collections

A Collection is used to represent a set of similar data items as a single unit that is used to group and manage related objects.

They contain data structures that are used to manipulate and store data efficiently. Python collections module provides a lot of data structures to implement different types of collections.

The following are the types of Collections that we’ll be looking at in this article:

  • OrderedDict
  • defaultdict
  • counter
  • namedtuple
  • deque
  • ChainMap

1.1. OrderedDict

Python OrderedDict maintains the order of insertion of elements through the key-value pairs in the Dictionary. If the user tries to insert a key again, it overwrites the previous value for that key. In OrderedDict, the order of items is strictly maintained i.e. the order of the elements served as output would be the same as the order of insertion of elements.

Example:

OrderedDict
OrderedDict

Output:

Output-OrderedDict
Output-OrderedDict

1.2. DefaultDict

DefaultDict in python is a container in the collection class. It is similar to the Dictionary container, the only difference is that a defaultdict will have a default value if the key is not set. Thus, we can group items belonging to the same key.

Example:

DefaultDict
DefaultDict

Output:

Output DefaultDict
Output DefaultDict

The key – Safa was used twice and the values associated with it were collected altogether when printed.


1.3. Counter

The Counter Collections keep a count of all the elements inserted in the collection along with the keys. It is a sub-class of Dictionary and used to track the items.

Example:

Counter
Counter

Output:

Output Counter
Output Counter

1.4.  Named Tuple

Namedtuple enables the user to provide names/tags to the elements. Thus, making the element accessible by index value or by name.

Hence, it enhances the readability of the code and is immutable.

Example:

Le’ts create a Tuple in Python.

Tuple
Tuple

Output:

Output Tuple
Output-Tuple

For better readability and understanding, we can create Named Tuple by assigning names to the items present in the Tuple.

Example:

NamedTuple
NamedTuple

Output:

Output NamedTuple
Output-NamedTuple

1.5. Deque

Deque is a double-ended queue that allows the user to add and delete elements from both the ends. It adds and enhances to the capabilities and functionalities of a stack or a queue.

Example:

Deque
Deque

Output:

Output Deque
Output Deque

1.6. ChainMap

ChainMap returns a chain/list of dictionaries and represent it as a single view mapping. ChainMap works well when there are different dictionaries with several key-value pairs in it, in such case, ChainMap represents them as a Single list/structure of all the dictionaries.

Example:

from collections import ChainMap
x = { 1: 'Engineering' , 2: 'Medical'}
y = {3: 'Analytics' , 4: 'Python'}
z = ChainMap(x,y)
print(z)

Output:

Output-ChainMap
Output-ChainMap

References