Learn Python – Python Collection Module- Basic and advance

The Python collection module is defined as a container that is used to save collections of data, for example – list, dict, set, and tuple, etc. It used to be introduced to enhance the functionalities of the built-in collection containers.

Python series module was first introduced in its 2.4 release.

There are unique types of series modules which are as follows:

namedtuple()

The Python namedtuple() feature returns a tuple-like object with names for each role in the tuple. It was used to eliminate the problem of remembering the index of each subject of a tuple object in everyday tuples.

Examples

pranshu = ('James', 24, 'M')    
print(pranshu)    

Output:

('James', 24, 'M')

OrderedDict()

The Python OrderedDict() is comparable to a dictionary object the place keys maintain the order of insertion. If we strive to insert key again, the preceding fee will be overwritten for that key.

Example

import collections    
d1=collections.OrderedDict()    
d1['A']=10    
d1['C']=12    
d1['B']=11    
d1['D']=13    
    
for k,v in d1.items():    
    print (k,v)    

Output:

A 10
C 12
B 11
D 13

defaultdict()

The Python defaultdict() is defined as a dictionary-like object. It is a subclass of the built-in dict class. It gives all techniques supplied by using dictionary but takes the first argument as a default information type.

Example

from collections import defaultdict      
number = defaultdict(int)      
number['one'] = 1      
number['two'] = 2      
print(number['three'])    

Output:

0

Counter()

The Python Counter is a subclass of dictionary object which helps to depend hashable objects.

Example

from collections import Counter      
c = Counter()    
list = [1,2,3,4,5,7,8,5,9,6,10]      
Counter(list)    
Counter({1:5,2:4})      
list = [1,2,4,7,5,1,6,7,6,9,1]      
c = Counter(list)      
print(c[1])     

Output:

3

deque()

The Python deque() is a double-ended queue which allows us to add and take away elements from both the ends.

Example

from collections import deque    
list = ["x","y","z"]    
deq = deque(list)    
print(deq)    

Output:

deque(['x', 'y', 'z'])

Chainmap Objects

A chainmap classification is used to businesses a couple of dictionary collectively to create a single list. The linked dictionary shops in the list and it is public and can be accessed by means of the map attribute. Consider the following example.

Example

from collections import ChainMap  
baseline = {'Name': 'Peter', 'Age': '14'}  
adjustments = {'Age': '14', 'Roll_no': '0012'}  
print(list(ChainMap(adjustments, baseline)))  

Output:

['Name', 'Age', 'Roll_no' ]

UserDict Objects

The UserDict behaves as a wrapper around the dictionary objects. The dictionary can be accessed as an attribute by means of using the UserDict object. It gives the easiness to work with the dictionary.

It provides the following attribute.

data – A real dictionary used to save the contents of the UserDict class.

UserList Objects

The UserList behaves as a wrapper classification round the list-objects. It is beneficial when we prefer to add new performance to the lists. It gives the easiness to work with the dictionary.

It provides the following attribute.

information – A actual listing is used to shop the contents of the User class.

UserString Objects

The UserList behaves as a wrapper class around the listing objects. The dictionary can be accessed as an attribute through using the UserString object. It gives the easiness to work with the dictionary.

It provides the following attribute.

facts – A real str object is used to store the contents of the UserString class.