Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconCollection In Python : Everything You Need to Know

Collection In Python : Everything You Need to Know

Last updated:
9th Apr, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
Collection In Python : Everything You Need to Know

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

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.

Profile

Arjun Mathur

Blog Author
Arjun is Program marketing manager at UpGrad for the Software development program. Prior to UpGrad, he was a part of the French ride-sharing unicorn "BlaBlaCar" in India. He is a B.Tech in Computers Science from IIT Delhi and loves writing about technology.

Frequently Asked Questions (FAQs)

1Why do we need the collection module in Python?

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.
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.

2What is the difference between a dictionary and an Ordered Dictionary in Python?

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.
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.

3What are the various operations of namedtuple?

The namedtuple in Python performs various operations. The following is a list of some of the most common operations performed by the 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.

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905267
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20926
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5068
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5179
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17647
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10803
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80780
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
139138
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon