Python is a powerful programming language. It is modular and known for its simplicity, reusability, and maintainability. In modular programming, a large and complex programming task is broken down into smaller modules.
This article talks about the module collection in Python. Collection improves the functionalities and is the alternative to Python’s general-purpose built-in dict, list, set, and tuple containers.
‘Python lists, tuples, sets, and dictionaries are user-defined data structures, and each come with their own set of advantages and disadvantages.’
Python Module
A module is a file containing Python definitions and statements that implement a set of functions. The import command is used to import modules from other modules. Python has several inbuilt modules.
Let us now come to the crux of the article and learn the module collection in Python in detail.
Collection in Python
Collection in Python is the container that stores collections of data. List, set, tuple, dict, etc., are inbuilt collections in Python. There are six collection modules in Python that offer additional data structures for storing data collections. These Python modules enhance the operations of the collection of built-in containers.
We will now discuss the different containers provided by the collection in the Python module.
1. OrderedDict
The OrderedDict() works similar to the dictionary object where keys maintain the order in which they are inserted. If you want to insert the key again, the previous value will be overwritten, and the key position will not change.
Example:
import OrderedDict from collections
d1=collections.OrderedDict()
d1[‘A’]=1
d1[‘B’]=2
d1[‘C’]=3
d1[‘D’]=4
for x,v in d1.items():
print (x,v)
Output:
A 1
B 2
C 3
D 4
2. deque()
The Python deque() is an optimized list that adds and removes items from both extremes.
Example:
import deque from collections
list1 = [“a”,”b”,”c”]
deq = deque(list1)
print(deq)
Output:
deque([‘a’, ‘b’, ‘c’])
Top Data Science Skills to Learn to upskill
SL. No
Top Data Science Skills to Learn
1
Data Analysis Online Courses
Inferential Statistics Online Courses
2
Hypothesis Testing Online Courses
Logistic Regression Online Courses
3
Linear Regression Courses
Linear Algebra for Analysis Online Courses
3. Counter
Counters are the subgroup of the dictionary objects that count hashable objects. The counter function takes input iterable as the argument and returns an output as a Dictionary. The key is an iterable element, and the value is the total number of times an element is present in the iterable.
Example:
import Counter from collections
c = Counter()
list1 = [1,2,3,4,5,7,8,5,9,6,10]
Counter(list1)
Counter({1:5,2:4})
list1 = [1,2,4,7,5,1,6,7,6,9,1]
c = Counter(list1)
print(c[1])
Output:
3
Additional Counter Functions
1. elements() Function
The elements() function returns a list of the elements present in the Counter object.
Example:
c = Counter({1:4,2:3})
print(list(c.elements()))
Output:
[1,1,1,1,2,2,2]
Here, a Counter object is created using a dictionary argument. The number of counts for 1 is 4, and for 2 is 3. The function elements() is called with the c object returning an iterator.
2. most_common() Function
The Counter() Python function returns an unordered dictionary while the most_common() function sorts it as per the number of each element count.
Example:
list = [1,2,3,4,5,5,5,7,3]
c = counter(list)
print(c.most_common())
Output:
[((5,3), (1,1),(2,1),(3,2),(4,1), (7,1))]
Here, the most_common function returns a sorted list as per the count of the elements. 5 comes three times; hence, it comes first, as the element of the list.
Explore our Popular Data Science Courses
Our learners also read: Free Online Python Course for Beginners
3. Subtract() Function
The subtract() considers iterable or mapping arguments and subtracts element count with that argument.
Example:
c = counter({1:2,2:3})
output= {1:1,2:1}
c.subtract(output)
print(c)
Output:
Counter({1:1,2:2})
4. Chainmap Objects
Chainmap class groups multiple dictionaries to create a single list. The linked dictionary is public and can be accessed by the map attribute.
Example:
Import chainmap from collections
dict1 = { ‘w’ : 1, ‘x’ : 2 }
dict2 = {‘y’ : 3. ‘z’: 4 }
chain_map = ChainMap(dict1,dict2)
print(chain_map.maps)
Output:
[{‘x’ : 2, ‘w’ :1}, {‘y’ : 3, ‘x’:4}]
5. Namedtuple
The namedtuple() function returns a tuple object with names for each position in the tuple. It was introduced to eliminate the problem of remembering the index of each field of a tuple object.
Example:
Import namedtuple from collections
Student = namedtuple (‘Student’,’firstname, lastname ,age’)
s1 = Student (‘Tom’, ‘Alter’, ‘12’)
print(s1.firstname)
Output:
Student(firstname=’Tom’, lastname=’Alter’, age=’12’)
In this example, you can access the fields of any instance of a class.
6. DefaultDict
The Python defaultdict() is a dictionary object and is a subclass of the dict class. It provides all dictionary methods but takes the first argument as a default data type. It throws an error when you access a non-existent key.
Example:
import defaultdict from collections
num = defaultdict(int)
num[‘one’] = 1
num[‘two’] = 2
print(num[‘three’])
Output:
0
Read our popular Data Science Articles
upGrad’s Exclusive Data Science Webinar for you –
Watch our Webinar on How to Build Digital & Data Mindset?
Conclusion
Collection in Python is known to bring improvement to the Python collection module. The collections were introduced in the 2.4 version of Python. A lot of changes and improvements can be expected in the subsequent versions. In this article, we have explained the six existing collections in Python with examples and the way they are implemented in the language. They are one of the most important concepts from a learner’s point of view.
Learn collection in Python with upGrad’s exclusive program of bachelor of computer applications. This program covers the necessary skills needed to make an entry into the IT industry.
If you are curious to learn about python, Data Science, check out IIIT-B & upGrad’s Executive PG Program in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.
Lear data science courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
Python already has 4 built-in collection data types. These are list, tuple, dictionary, and set. However, these data containers are used for general purposes.
Python Dictionary or “Dict” is an inbuilt data structure of Python that is used to store an unordered collection of elements. Unlike other Python data structures that store single values, the dictionary data structure stores key-value pairs where every key is unique. It does not remember the insertion order of key-value pairs and iterates through the keys.
The namedtuple in Python performs various operations. The following is a list of some of the most common operations performed by the namedtuple.Why do we need the collection module in Python?
The following points highlight the major advantages of using the collection module over built-in data containers.
The collection module provides the specialized version of these containers, such as namedtuple, OrderedDict, defaultdict, chainmap, counter, and many more.
Being more optimized, these containers prove to be a better alternative to the traditional data containers such as list, tuple and set.
The collection module is efficient to deal with structured data.
Data containers like namedtuple consume less memory and provide enhanced operations to store and manage the data. What is the difference between a dictionary and an Ordered Dictionary in Python?
On the other hand, an Ordered Dictionary or OrderedDict keeps a track of the insertion order of key-value pairs. It also consumes more memory than a regular dictionary in Python due to its doubly linked list implementation. If you delete and re-insert the same key, it will be inserted in its original position as an OrderedDict remembers the insertion order. What are the various operations of namedtuple?
1. Access Operations: Access by index: The elements in a namedtuple can be accessed by their indices, unlike a dictionary. Access by key name: The alternative way to access the elements is by their key name.
2. Conversion Operations: make(): This function returns a namedtuple. _asadict(): This function returns an ordered dictionary that is constructed from the mapped values. using “**” (double star) operator: This function converts a Python dictionary into a namedtuple.
3. Additional Operations: _fileds(): This function returns all the key names of the given namedtuple. _replace(): This function takes a key name as its argument and changes the values mapped to it.