Updated On : May-09,2020 Time Investment : ~15 mins

We have already read about the different types of collection data types in Python programming language - List, Tuple, Sets, Dictionary. Here is one more built-in module known as collection which has a specialized data structures. It basically covers for the shortcoming of the 4 data types. In this python tutorial, we will go through each of those specialized data structures in detail.

What Do You Mean By Collections In Python?

Definition: Collections in python are basically container data types, namely lists, sets, tuples, dictionary. They have different characteristics based on the declaration and the usage.

  • A list is declared in square brackets, it is mutable, stores duplicate values and elements can be accessed using indexes.

  • A tuple is ordered and immutable in nature, although duplicate entries can be there inside a tuple.

  • A set is unordered and declared in square brackets. It is not indexed and does not have duplicate entries as well.

  • A dictionary has key value pairs and is mutable in nature. We use square brackets to declare a dictionary.

These are the python’s general purpose built-in container data types. But as we all know, python always has a little something extra to offer. It comes with a python module named collections which has specialized data structures.

Specialized Collection Data Structures

Collections module in python implements specialized data structures which provide alternative to python’s built-in container data types. Following are the specialized data structures in collections module.

  • namedtuple( )
  • deque
  • Chainmap
  • Counter
  • OrderedDict
  • defaultdict
  • UserDict
  • UserList
  • UserString

namedtuple( )

Definition: It returns a tuple with a named entry, which means there will be a name assigned to each value in the tuple. It overcomes the problem of accessing the elements using the index values. With namedtuple( ) it becomes easier to access these values, since you do not have to remember the index values to get specific elements.

from collections import namedtuple
a = namedtuple('courses' , 'name , tech')
s = a('data science' , 'python')
print(s)

#the output will be courses(name='python' , tech='python')
courses(name='data science', tech='python')
s._make(['data science' , 'python'])
#the output will be same as before.
courses(name='data science', tech='python')

deque

Definition: deque pronounced as ‘deck’ is an optimized list to perform insertion and deletion easily.

Understanding 'deque' With The Help Of Example

#creating a deque
from collections import deque

a = ['d' , 'u' , 'r' , 'e' , 'k']
a1 = deque(a)
print(a1)
#the output will be deque([ 'd' , 'u' , 'r' , 'e' , 'k' ])
deque(['d', 'u', 'r', 'e', 'k'])

You can Insert as well as remove the elements in a 'deque'.

Inserting An Element

a1.append('a')
print(a1)
# the output will be deque([ 'd' , 'u' , 'r' , 'e' , 'k' , 'a' ])
a1.appendleft('e')
print(a1)
# the output will be deque(['e' , 'd' , 'u' , 'r' , 'e' , 'k' , 'a' ])
deque(['d', 'u', 'r', 'e', 'k', 'a'])
deque(['e', 'd', 'u', 'r', 'e', 'k', 'a'])

Removing An Element

a1.pop()
print(a1)
#the output will be deque([ 'e' , 'd' , 'u' , 'r' , 'e' , 'k' ])
a1.popleft()
print(a1)
#the output will be deque([ 'd' , 'u' , 'r' , 'e' , 'k' ])
deque(['e', 'd', 'u', 'r', 'e', 'k'])
deque(['d', 'u', 'r', 'e', 'k'])

ChainMap

Definition: It is a dictionary like class which is able to make a single view of multiple mappings. It basically returns a list of several other dictionaries. Suppose you have two dictionaries with several key value pairs, in this case ChainMap will make a single list with both the dictionaries in it.

from collections import ChainMap
a = { 1: 'edureka' , 2: 'python'}
b = {3: 'data science' , 4: 'Machine learning'}
c = ChainMap(a,b)
print(c)
#the output will be ChainMap[{1: 'edureka' , 2: 'python'} , {3: 'data science' , 4: 'Machine learning'}]
ChainMap({1: 'edureka', 2: 'python'}, {3: 'data science', 4: 'Machine learning'})

To Add New Dictionary In Chainmap

a1 = { 5: 'AI' , 6: 'neural networks'}
c1 = c.new_child(a1)
print(c1)
#the output will be ChainMap[{1: 'edureka' , 2: 'python'} , {3: 'data science' , 4: 'Machine learning'}, { 5: 'AI' , 6: 'neural networks'}]
ChainMap({5: 'AI', 6: 'neural networks'}, {1: 'edureka', 2: 'python'}, {3: 'data science', 4: 'Machine learning'})

Counter

Definition: It is a dictionary subclass which is used to count hashable objects.

from collections import Counter
a = [1,1,1,1,2,3,3,4,3,3,4]
c = Counter(a)
print(c)
#the output will be Counter = ({1:4 , 2:1 , 3:4 , 4:2})
Counter({1: 4, 3: 4, 4: 2, 2: 1})

If you wish to perform insertion of element or remove an element from the Counter, you need to work with three different functions.

  • element function – It returns a list containing all the elements in the Counter.
  • Most_common( ) – It returns a sorted list with the count of each element in the Counter.
  • Subtract( ) – It takes an iterable object as an argument and deducts the count of the elements in the Counter.

OrderedDict

Definition: It is a dictionary subclass which remembers the order in which the entries were added. Basically, even if you change the value of the key, the position will not be changed because of the order in which it was inserted in the dictionary.

from collections import OrderedDict
od = OrderedDict()
od[1] = 'e'
od[2] = 'd'
od[3] = 'u'
od[4] = 'r'
od[5] = 'e'
od[6] = 'k'
od[7] = 'a'
print(od)
#the output will be OrderedDict[(1 , 'e'), (2 , 'd') , (3 , 'u'), (4 , 'r'), (5 , 'e'), (6 , 'k'), (7 , 'a')]
OrderedDict([(1, 'e'), (2, 'd'), (3, 'u'), (4, 'r'), (5, 'e'), (6, 'k'), (7, 'a')])

defaultdict

Definition: It is a dictionary subclass which calls a factory function to supply missing values. In general, it does not throw any errors when a missing key value is called in a dictionary.

from collections import defaultdict
d = defaultdict(int)
#we have to specify a type as well.
d[1] = 'edureka'
d[2] = 'python'
print(d[3])
#it will give the output as 0 instead of keyerror.
0

UserDict

This class acts as a wrapper around dictionary objects. The need for this class came from the necessity to subclass directly from dict. It becomes easier to work with this class as the underlying dictionary becomes an attribute.

class collections.UserDict([initialdata])

UserList

Definition: This class acts like a wrapper around the list objects. It is a useful base class for other list like classes which can inherit from them and override the existing methods or even add a fewer new ones as well.

The need for this class came from the necessity to subclass directly from list. It becomes easier to work with this class as the underlying list becomes an attribute.

class collections.UserList([list])
Dolly Solanki  Dolly Solanki

Share Views Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.