Collections in Python

Jagpreet Kaur
AI Perceptron
Published in
5 min readDec 19, 2020

As we know python also comes with a built-in module known as collections.

Types of Advanced Data Structure in Python

There are specialized data collections in python which are basically container data types like lists, sets, tuples, dictionary store collection of data as we have seen above.

Collections are also implemented in specialized data structures.

  1. Counter.
  2. Chainmap
  3. deque
  4. OrderedDict
  5. UserList
  6. UserString
  7. DefaultDict
  8. namedtuple( )

1.Counter.

As we know counter is a subclass of dictionary objects. Counter is a container of collections module.

[A container is an object which holds other objects, they provide a way to access the contained object. Build-in containers are tuple, list, set, dictionary, and remaining comes under collections module.]

We have to import the Counter class before we can create a Counter instance.

2.ChainMap

ChainMap is used to combine several dictionaries into a single unit or mappings and returns a list of dictionaries.

Values from ChainMap can be accessed using the key name. They can also be accessed by using the keys() and values() method.

3. Deque( )

Deque (Double Ended Queue) where insertion and deletion can be done from both the ends. It gives O(1) time complexity as compared to list ie. O(n) as insertion and deletion can be done from both the ends.

Deque objects support the following methods:

append(A)
It adds ‘ A’ to the right side of the deque.

appendleft(A)
It adds ‘A’ to the left side of the deque.

pop()
It removes the element from the right side of the deque and returns remaining deque elements.

popleft()
It removes the element from the left side of the deque and returns remaining elements present.

clear()
It removes all the elements from the deque leaving it with length 0.

count(A)
It counts the number of deque elements present.

remove(value)
It removes the first occurrence of value. If not found, gives a ValueError.

reverse()
It reverses the elements and then returns.

output:

4.OrderedDict

OrderedDict is a dictionary subclass that remembers the order entries that were added in sequence.

The dictionary and orderdict are almost the same only difference is ordereddict tracks the order of key inserted which dictionary will not track.

Let see the example of orderedDict:

output:

Example 2: For deletion of the element and again adding it, the inserted element will be added at the back of the ordereddict.

output:

5.UserList

List in Python will be ordered and also have a definite count. Python support a list like container called UserList .In the Userlist, data should be string, int, float, etc.

It is used to add new functionality to create our own list.

output:

6.UserString

A String like a container is called as Userstring available in collection modules. This type of module is useful when you want to create new functionality in string of your own and it can be accessed by the data attribute of your class.

let see the example.

output:

7.DefaultDict

In the functionality of both dictionaries and DefaultDict, there is not a lot of difference except that defualtdict never adds to a KeyError. It provides a default value for the key that does not exist.

Here is an example using a dictionary:

To handle this error we use DefaultDict objects, which can be initialized using DefaultDict() method by passing the data type as an argument.

output: In the output, you can see that it prints the default statement ‘not present’ in the last value[Z] as it’s value was not defined.

8. Namedtuple

Namedtuple() assigns named_field to each tuple position so as to make it more readable. It can be applied to regular tuples anywhere. It adds the ability to access using name instead of position index.

It is similar to a dictionary as they also contain keys that are based on a particular value. But on contrary, it supports both access from key-value and iteration as well which the dictionaries lack.

Here is an example of Namedtuple:

There are also some more operations in Namedtuple like

Conversion Operations: _make() , _asdict() , “**” (double star) operator.

Additional Operations :_field,_replace().

--

--