View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Collection In Python : Everything You Need to Know

By Arjun Mathur

Updated on Nov 29, 2022 | 6 min read | 5.5k views

Share:

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’])

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.

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

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree18 Months
View Program

Placement Assistance

Certification8-8.5 Months
View Program

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.

Frequently Asked Questions (FAQs)

1. Why do we need the collection module in Python?

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

3. What are the various operations of namedtuple?

Arjun Mathur

57 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

View Program
Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

18 Months

View Program
IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months

View Program