Member-only story
Programming
8 Active Learning Insights of Python Collection Module
Container for data collections
Collection Module
The python collection module is a container used to store many data collections like objects and provide access to these contained objects. List, tuple, and dictionary are some of the built-in containers. These modules were mainly to improve the functionalities of these built-in collection containers. Let us have a look at some of the specialized types of containers provided by the collections module.
Topics to be covered
1. Counters
2. Ordered Dic
3. Default Dic
4. Chain Map
5. Named Tuple
6. DeQue
7. User Dic
8. User List
9. User String
Counters
A counter is a dictionary subclass that we use for counting hashable objects.
Syntax: class collections.Counter([iterable-or-mapping])
Example:
from collections import Counternumbers = [9, 5, 7, 4, 7, 10, 9, 10, 3, 5, 3, 6, 8, 4, 5, 10, 5]
Count = Counter(numbers)print(Count)
print(list(Count.elements()))
print(Count.most_common())sub = {10:1, 9:2}
print(Count.subtract(sub))
print(Count.most_common())