Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Collections in Python

Collections in Python

January 24th, 2020

Collections in Python

In this tutorial, we are going to learn about “Collections in Python”. Collections are container data types such as lists, tuples, sets, and dictionaries. These data types are called as general-purpose data types. Python provides collection data types namely

  • List
  • Sets
  • Tuple and
  • Dictionary.

Depending on how we declare them and use them, these data types respond differently. Let’s know about them.

  • Lists are usually declared within square brackets. They are variables and therefore unpredictable. Lists store the duplicate values. Elements present in the lists can be accessed through indexes.
  • Tuples cannot vary and therefore predictable. Tuples are always ordered. Similar to lists, tuples can also store duplicate values.
  • Sets are also declared within square brackets. They are unordered. They are not indexed and also does not carry duplicate values.
  • Dictionaries are also declared within square brackets. They usually vary. They contain key-value pairs.

Since these general-purpose data types cause several disadvantages, there is a built-in support module available in Python which is called collections. This in-built module is a special type of data structure available in Python.

Special Type of Data Structures in Python:

Special Data structures in Python serves as an alternative to the general-purpose container data types. As we discussed already, it is an in-built module available in Python. Collections module contains the following are the special data structures in python:

  • namedtuple()
  • Chainmap
  • Counter
  • Deque
  • DefaultDict
  • OrderedDict
  • UserDict
  • UserList and
  • UserString

Let’s see the explanation and working of this specialized data structure one by one:

namedtuple():

This data type returns a tuple that contains a named entry which means that each value in the tuple contains a name. namedtuple() does not access every element in the tuple using index values. This avoids remembering those index values to access the elements.

Working of namedtuple():

To use the namedtuple() data type, we must import the in-built collections module present in python. A simple import command does the task, as follows:

From collections import namedtuple
Later, we can use this data type to perform the functionality we need. A sample instance details how namedtuple() can work.
i = namedtuple(‘tutorial’, ‘name, domain’)
j = i(‘Machine Learning’, ‘python’)
print(j)
Output:
tutorial(name=’Machine Learning’, domain= ‘python’)
To create a namedtuple using a list, we can write as:
j._make([‘Machine Learning’, ’python’])

We will get the same output for this command what we got above.

ChainMap:

This class functions like a dictionary that makes several mappings to a single view. Generally, a chain map lists other dictionaries as well. Say for example; consider you have two dictionaries that contain multiple key-value pairs. Chain map will make multiple key-value pairs to a single list with both the dictionaries within.

Working of ChainMap:

From collections import ChainMap                                       
i = { 1: ‘Training’, 2: ‘Machine Learning’}
j = {3: ‘AWS’, 4: ‘Ethical Hacking’}
k = ChainMap(i,j)
print(k)
Output:
ChainMap[{ 1: ‘Training’, 2: ‘Machine Learning’}, {3: ‘AWS’, 4: ‘Ethical Hacking’}]
We can use keys as an index to access or insert the elements. And, it is also possible to add a new dictionary to a current chain map. The following method shows how to add a new dictionary:
i1 = {5: ‘Python’, 6: ‘Selenium’}
j1= j.new_child(i1)
print(j1)
Output:
ChainMap[{ 1: ‘Training’, 2: ‘Machine Learning’}, {3: ‘AWS’, 4: ‘Ethical Hacking’},{5: ‘Python’, 6: ‘Selenium’}]

Counter:

The counter is a subclass in a dictionary that counts hashable objects.
Working of Counter:

From collections import Counter
i = [11,11,12,11,12,11,13,12,14,13,11,14]
j = Counter(i)
print(j)
Output:
Counter = ({11:5, 12:3, 13:2, 14:2})
We can also perform the following operations using a Counter:

  • Element Function: This function returns a list that contains the entire elements present in the counter.
  • Most_common(): This returns a sorted list that contains the count of each element present in the counter.
  • Subtract(): subtract() deducts the count of the elements present in the counter through an iterable object as an argument.

Deque:

Deque is an optimized list that is used to do insertion or deletion of items. It is also called as ‘deck’.

Working of Deque:

From collections import deque
i = [’y’,’t’,’h’,’o’]
j = deque(i)
print(j)

Output:

deque([’y’,’t’,’h’,’o’])
Let’s see how to insert items to a deque
j.append(‘n’)
print(j)

Output:

deque([’y’, ’t’,’h’,’o’,’n’])
The above script appends to the right. We can also append to the left.
j.appendleft(‘p’)
print(j)

Output:

deque([‘p’,’y’,’t’,’h’,’o’,’n’])
Similar to insertion of items, it is also possible to delete items using deque. Let’s see how to do it.
j.pop()
print(j)

Output:

deque([‘p’,’y’,’t’,’h’,’o’])

 

Input: 

j.popleft()
print(j)

Output:

deque([’y’,’t’,’h’,’o’])

DefaultDict:

It is a subclass present in the dictionary that calls a factory function to provide missing values. Generally, if a missing key value is called in a dictionary, it does not return any error.

Working on DefaultDict

From collections import defaultdict
a= defaultdict(int)
a[1] = ‘training’
a[2] = ‘java’
print(a[3])
The above script does not return any error, rather it will output ‘0’

Ordered Dict:

This is also a subclass in the dictionary that remembers the sequence in which the entries are inputted. Though you change the key value, the position of the item cannot change because of the order in which the item has been inserted into the dictionary.

Working of  Ordered Dict:

From collections import OrderedDict
seq = OrderedDict()
seq[1] = ‘t’
seq[2] = ‘r’
seq[3] = ‘a’
seq[4] = ‘i’
seq[5] = ‘n’
seq[6] = ‘i’
seq[7] = ‘n’
seq[8] = ‘g’
print(seq)

Output:

OrderedDict[(1,’t’), (2,’r’), (3,’a’), (4,’i’), (5,’n’), (6,’i’), (7,’n’), (8,’g’),

Output:

OrderedDict[(1,’t’), (2,’r’), (3,’a’), (4,’i’), (5,’n’), (6,’i’), (7,’n’), (8,’g’),
As we already mentioned, OrderedDict remembers the sequence in which the key values were inserted. Changing the value of the key will not affect the output. Say, for example, if you change the key value of 2 to 5, the output will remain unchanged.

UserDict:

This class functions as a wrapper to the dictionary objects. In UserDict, the prevailing dictionary acts as an attribute and thus makes tasks easier.

Class collections.UserDict([primarydata])

UserDict simulates a dictionary. The content of the instance was stored in a regular dictionary that can be accessed through the attribute ‘data’ of the class ‘UserDict’. But, the reference of the primary data will not be considered but can be used for some other purposes.

UserList

This class functions as a wrapper to the list-objects. UserList will be useful for other lists like classes that are inherited from this class and surpass the existing methods. You can also add new ones as well. Similar to UserDict, the prevailing dictionary in UserList acts as an attribute and thus makes tasks easier.

Class collections.UserList([List])

UserList simulates a list. The content of the instance was stored in a customized list. The subclasses of the list depend on to provide a constructor that can be called with no or one contention. With this, the tutorial about the collection module in python ends.  See you all in the next tutorial.

Related Blogs