Introduction to Collection Module in Python
In Python, the module is described as a small Python program or a file containing definitions, variables, statements that are used to implement a set of functions or methods. Python collection module was introduced in Python version 2.4. Python collection module is defined as the container that collects or stores data collection such as list, dict, set, tuple, etc. To improve the functionalities of built-in collection containers, Python introduced a collections module. As there are many different modules that provide additional data structures to store the set of data, and one such module is Python’s collections module. The collections module is an alternative to Python’s general-purpose built-in containers.
Top 5 Data Structures of Collection Module in Python
In this article, we will be discussing the most common data structures that are used from the Python collections module. There are different data structures in the collections modules such as Counter, deque, orderdict, defaultdict, namedtuple, chainmap, userdict, userlist, userstring, etc. Let us discuss a few of them below in detail:
1. The Counter
The counter is a dict subclass that is a dictionary subclass used to count the hashtable objects. The counter acts like a dictionary in which one element is a key that is iterable, and the value is the number of times the key element is present in that dictionary. So in the collections module, the counter() function takes a key as an argument, and it returns a dictionary. Let’s see how it works:
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:
Explanation: In the above code counter object, ‘c’ is created, and counter() function can take dictionary or list as an argument so the key: count of the value of a key is printed so when c[1] is asked to print it will print how many times ‘1’ has appeared in the list. So it returns ‘3’ as output.
2. Namedtuple()
As the function name itself says, it returns the tuple with the named entry. So, in general, each value in the tuple is given a name in which it can be easy to access these values in the tuple most of the time; it is done with indexing, but it tends to forget the index numbers which will be difficult to access the values. Hence it is easy with named value in the tuple to access the values.
Example:
from collections import namedtuple
Student = namedtuple('Student', 'fname, lname, age')
s1 = Student('Sita', 'Patil', '10')
print(s1.fname)
Output:
Explanation: In the above example, the namedtuple creates a ‘Student’ object, so using the instance ‘s1’ of class ‘Student’, we can access any field in that object such as ‘fname’ so it returns as ‘Sita’, which is as in the program.
3. Deque()
So this also is a queue or a list optimized for adding and removing elements from both the ends of the queue. So deque is created using the deque() constructor.
Example:
from collections import deque
list = ["x","y","z"]
deq = deque(list)
print(deq)
deq.append("d")
print(deq)
deq.pop()
deq.popleft()
print(deq)
list = ["a","b","c"]
deq = deque(list)
print(deq)
print(deq.clear())
Output:
Explanation: In the above program, it deque creates an object ‘deq’ which will first print the list and then the items are added by using deq.append(), items to remove from right deq.pop() and to remove from left deq.popleft() and to clear the entire list then it uses deq.clear().
4. Defaultdict
As it says, it is similar to the dictionary, but it will not see if there is proper key is given to access if the not correct key is declared, then it will not throw any error; instead, it will initialize the key element of the data type that you pass as the argument, and this data type is called default_factory. It behaves like a dictionary, but it takes the first argument as a default data type.
Example:
from collections import defaultdict
n = defaultdict(int)
n['a'] = 1
n['b'] = 2
print(n['c'])
Output:
Explanation: In the above code, the defaultdict() creates ‘n’ as an object that is assigned a key ‘a’ with value ‘1’, ‘b’ with value ‘2’, and the key ‘c’ has not been assigned any value so it will by default prints 0 as value for that key.
5. Orderdict
As the name itself suggests, this is a dictionary in which keys are in order in which they were inserted, so the position of the key will not change if you change the value. This means if insert the key again, then the previous value for that key will be overwritten.
Example:
from collections import OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
print(od)
for k, v in od.items():
print(k, v)
Output:
Explanation: In the above code, the object ‘od’ is created from the ‘OrderDict()’, which will assign each key-value, and that will be printed in the same order in the output with their respective key and values are printed as a result in an order.
Conclusion
So to conclude this article, as discussed in this article, the collection module in Python is nothing but the container that collects and stores the data with different data types like dict, list, tuple, set, etc. There are many different data structures used in the collections module. To use all these data structures, we need to import the collections module in Python, which in detail each data structure has different functions and they work differently. These can work on the numbers or characters, or string. As per the survey, they say Java has a very well collection library than Python, so it says that it needs to be improved.
Recommended Articles
This is a guide to Collection Module in Python. Here we discuss the basic concept and top 5 data structures of the Collection Module in Python and different examples and code implementation. You may also look at the following articles to learn more –
24 Online Courses | 14 Hands-on Projects | 110+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses