Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Namedtuple in Python
Next article icon

Python Collections Module

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The collection Module in Python provides different types of containers. A Container is an object that is used to store different objects and provide a way to access the contained objects and iterate over them. Some of the built-in containers are Tuple, List, Dictionary, etc. In this article, we will discuss the different containers provided by the collections module.

Table of Content

  • Counters
  • OrderedDict
  • DefaultDict
  • ChainMap
  • NamedTuple
  • Deque
  • UserDict
  • UserList
  • UserString


Counters

A counter is a sub-class of the dictionary. It is used to keep the count of the elements in an iterable in the form of an unordered dictionary where the key represents the element in the iterable and value represents the count of that element in the iterable.

Note: It is equivalent to bag or multiset of other languages.

Syntax:

class collections.Counter([iterable-or-mapping])

Initializing Counter Objects

The counter object can be initialized using the counter() function and this function can be called in one of the following ways:

  • With a sequence of items
  • With a dictionary containing keys and counts
  • With keyword arguments mapping string names to counts

Example:

Python
# A Python program to show different  # ways to create Counter  from collections import Counter     # With sequence of items   print(Counter(['B','B','A','B','C','A','B',                'B','A','C']))    # with dictionary  print(Counter({'A':3, 'B':5, 'C':2}))    # with keyword arguments  print(Counter(A=3, B=5, C=2)) 

Output:

Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})

Note: For more information, refer  Counters in Python.

OrderedDict

An OrderedDict is also a sub-class of dictionary but unlike dictionary, it remembers the order in which the keys were inserted. 

Syntax:

class collections.OrderDict()

Example:

Python
# A Python program to demonstrate working # of OrderedDict   from collections import OrderedDict     print("This is a Dict:\n")  d = {}  d['a'] = 1 d['b'] = 2 d['c'] = 3 d['d'] = 4    for key, value in d.items():      print(key, value)     print("\nThis is an Ordered Dict:\n")  od = OrderedDict()  od['a'] = 1 od['b'] = 2 od['c'] = 3 od['d'] = 4    for key, value in od.items():      print(key, value)  

Output:

This is a Dict:

a 1
b 2
c 3
d 4

This is an Ordered Dict:

a 1
b 2
c 3
d 4

While deleting and re-inserting the same key will push the key to the last to maintain the order of insertion of the key.

Example:

Python
# A Python program to demonstrate working # of OrderedDict   from collections import OrderedDict    od = OrderedDict()  od['a'] = 1 od['b'] = 2 od['c'] = 3 od['d'] = 4    print('Before Deleting') for key, value in od.items():      print(key, value)       # deleting element od.pop('a')  # Re-inserting the same od['a'] = 1  print('\nAfter re-inserting') for key, value in od.items():      print(key, value) 

Output:

Before Deleting
a 1
b 2
c 3
d 4

After re-inserting
b 2
c 3
d 4
a 1

Note: for more information, refer OrderedDict in Python

DefaultDict

A DefaultDict is also a sub-class to dictionary. It is used to provide some default values for the key that does not exist and never raises a KeyError.

Syntax:

class collections.defaultdict(default_factory)

default_factory is a function that provides the default value for the dictionary created. If this parameter is absent then the KeyError is raised.

Initializing DefaultDict Objects

DefaultDict objects can be initialized using DefaultDict() method by passing the data type as an argument.

Example:

Python
# Python program to demonstrate  # defaultdict          from collections import defaultdict          # Defining the dict  d = defaultdict(int)      L = [1, 2, 3, 4, 2, 4, 1, 2]      # Iterate through the list  # for keeping the count  for i in L:              # The default value is 0      # so there is no need to       # enter the key first      d[i] += 1         print(d)  

Output:

defaultdict(<class 'int'>, {1: 2, 2: 3, 3: 1, 4: 2})

Example 2:

Python
# Python program to demonstrate  # defaultdict        from collections import defaultdict        # Defining a dict  d = defaultdict(list)     for i in range(5):      d[i].append(i)         print("Dictionary with values as list:")  print(d)  

Output:

Dictionary with values as list: 
defaultdict(<class ‘list’>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})

Note: For more information, refer Defaultdict in Python

ChainMap

A ChainMap encapsulates many dictionaries into a single unit and returns a list of dictionaries.

Syntax:

class collections.ChainMap(dict1, dict2)

Example:

Python
# Python program to demonstrate  # ChainMap          from collections import ChainMap          d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} d3 = {'e': 5, 'f': 6}  # Defining the chainmap  c = ChainMap(d1, d2, d3)      print(c) 

Output:

ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6})

Accessing Keys and Values from ChainMap

Values from ChainMap can be accessed using the key name. They can also be accessed by using the keys() and values() method.

Example:

Python
# Python program to demonstrate  # ChainMap          from collections import ChainMap          d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} d3 = {'e': 5, 'f': 6}  # Defining the chainmap  c = ChainMap(d1, d2, d3)      # Accessing Values using key name print(c['a'])  # Accessing values using values() # method print(c.values())  # Accessing keys using keys() # method print(c.keys()) 

Output:

1 
ValuesView(ChainMap({‘a’: 1, ‘b’: 2}, {‘c’: 3, ‘d’: 4}, {‘e’: 5, ‘f’: 6})) 
KeysView(ChainMap({‘a’: 1, ‘b’: 2}, {‘c’: 3, ‘d’: 4}, {‘e’: 5, ‘f’: 6}))

Adding new dictionary

A new dictionary can be added by using the new_child() method. The newly added dictionary is added at the beginning of the ChainMap.

Example:

Python
# Python code to demonstrate ChainMap and  # new_child()     import collections     # initializing dictionaries  dic1 = { 'a' : 1, 'b' : 2 }  dic2 = { 'b' : 3, 'c' : 4 }  dic3 = { 'f' : 5 }     # initializing ChainMap  chain = collections.ChainMap(dic1, dic2)     # printing chainMap  print ("All the ChainMap contents are : ")  print (chain)     # using new_child() to add new dictionary  chain1 = chain.new_child(dic3)     # printing chainMap print ("Displaying new ChainMap : ")  print (chain1)  

Output:

All the ChainMap contents are : 
ChainMap({'a': 1, 'b': 2}, {'b': 3, 'c': 4})
Displaying new ChainMap :
ChainMap({'f': 5}, {'a': 1, 'b': 2}, {'b': 3, 'c': 4})

Note: For more information, refer ChainMap in Python

NamedTuple

A NamedTuple returns a tuple object with names for each position which the ordinary tuples lack. For example, consider a tuple names student where the first element represents fname, second represents lname and the third element represents the DOB. Suppose for calling fname instead of remembering the index position you can actually call the element by using the fname argument, then it will be really easy for accessing tuples element. This functionality is provided by the NamedTuple.

Syntax:

class collections.namedtuple(typename, field_names)

Example:

Python
# Python code to demonstrate namedtuple()    from collections import namedtuple    # Declaring namedtuple()  Student = namedtuple('Student',['name','age','DOB'])     # Adding values  S = Student('Nandini','19','2541997')     # Access using index  print ("The Student age using index is : ",end ="")  print (S[1])     # Access using name   print ("The Student name using keyname is : ",end ="")  print (S.name) 

Output:

The Student age using index is : 19
The Student name using keyname is : Nandini

Conversion Operations 

1. _make(): This function is used to return a namedtuple() from the iterable passed as argument.

2. _asdict(): This function returns the OrdereDict() as constructed from the mapped values of namedtuple().

Example:

Python
# Python code to demonstrate namedtuple() and  # _make(), _asdict()     from collections import namedtuple    # Declaring namedtuple()  Student = namedtuple('Student',['name','age','DOB'])     # Adding values  S = Student('Nandini','19','2541997')     # initializing iterable   li = ['Manjeet', '19', '411997' ]     # initializing dict  di = { 'name' : "Nikhil", 'age' : 19 , 'DOB' : '1391997' }     # using _make() to return namedtuple()  print ("The namedtuple instance using iterable is  : ")  print (Student._make(li))     # using _asdict() to return an OrderedDict()  print ("The OrderedDict instance using namedtuple is  : ")  print (S._asdict())  

Output:

The namedtuple instance using iterable is  : 
Student(name='Manjeet', age='19', DOB='411997')
The OrderedDict instance using namedtuple is :
OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])

Note: For more  information, refer NamedTuple in Python

Deque

Deque (Doubly Ended Queue) is the optimized list for quicker append and pop operations from both sides of the container. It provides O(1) time complexity for append and pop operations as compared to list with O(n) time complexity.

Syntax:

class collections.deque(list)

This function takes the list as an argument.

Example:

Python
# Python code to demonstrate deque     from collections import deque    # Declaring deque queue = deque(['name','age','DOB'])     print(queue) 

Output:

deque(['name', 'age', 'DOB'])

Inserting Elements

Elements in deque can be inserted from both ends. To insert the elements from right append() method is used and to insert the elements from the left appendleft() method is used.

Example:

Python
# Python code to demonstrate working of   # append(), appendleft()     from collections import deque     # initializing deque  de = deque([1,2,3])     # using append() to insert element at right end   # inserts 4 at the end of deque  de.append(4)     # printing modified deque  print ("The deque after appending at right is : ")  print (de)     # using appendleft() to insert element at right end   # inserts 6 at the beginning of deque  de.appendleft(6)     # printing modified deque  print ("The deque after appending at left is : ")  print (de)  

Output:

The deque after appending at right is : 
deque([1, 2, 3, 4])
The deque after appending at left is :
deque([6, 1, 2, 3, 4])

Removing Elements

Elements can also be removed from the deque from both the ends. To remove elements from right use pop() method and to remove elements from the left use popleft() method.

Example:

Python
# Python code to demonstrate working of   # pop(), and popleft()   from collections import deque  # initializing deque  de = deque([6, 1, 2, 3, 4])  # using pop() to delete element from right end   # deletes 4 from the right end of deque  de.pop()     # printing modified deque  print ("The deque after deleting from right is : ")  print (de)     # using popleft() to delete element from left end   # deletes 6 from the left end of deque  de.popleft()     # printing modified deque  print ("The deque after deleting from left is : ")  print (de)  

Output:

The deque after deleting from right is : 
deque([6, 1, 2, 3])
The deque after deleting from left is :
deque([1, 2, 3])

Note: For more information, refer Deque in Python.

UserDict

UserDict is a dictionary-like container that acts as a wrapper around the dictionary objects. This container is used when someone wants to create their own dictionary with some modified or new functionality. 

Syntax:

class collections.UserDict([initialdata])

Example:

Python
# Python program to demonstrate  # userdict         from collections import UserDict         # Creating a Dictionary where  # deletion is not allowed  class MyDict(UserDict):             # Function to stop deletion      # from dictionary      def __del__(self):          raise RuntimeError("Deletion not allowed")                 # Function to stop pop from       # dictionary      def pop(self, s = None):          raise RuntimeError("Deletion not allowed")                 # Function to stop popitem       # from Dictionary      def popitem(self, s = None):          raise RuntimeError("Deletion not allowed")         # Driver's code  d = MyDict({'a':1,      'b': 2,      'c': 3})    d.pop(1)  

Output:

Traceback (most recent call last):
File "/home/f8db849e4cf1e58177983b2b6023c1a3.py", line 32, in <module>
d.pop(1)
File "/home/f8db849e4cf1e58177983b2b6023c1a3.py", line 20, in pop
raise RuntimeError("Deletion not allowed")
RuntimeError: Deletion not allowed
Exception ignored in: <bound method MyDict.__del__ of {'a': 1, 'b': 2, 'c': 3}>
Traceback (most recent call last):
File "/home/f8db849e4cf1e58177983b2b6023c1a3.py", line 15, in __del__
RuntimeError: Deletion not allowed

Note: For more information, refer UserDict in Python

UserList

UserList is a list like container that acts as a wrapper around the list objects. This is useful when someone wants to create their own list with some modified or additional functionality.

Syntax:

class collections.UserList([list])

Example:

Python
# Python program to demonstrate  # userlist         from collections import UserList         # Creating a List where  # deletion is not allowed  class MyList(UserList):             # Function to stop deletion      # from List      def remove(self, s = None):          raise RuntimeError("Deletion not allowed")                 # Function to stop pop from       # List      def pop(self, s = None):          raise RuntimeError("Deletion not allowed")         # Driver's code  L = MyList([1, 2, 3, 4])     print("Original List")     # Inserting to List"  L.append(5)  print("After Insertion")  print(L)     # Deleting From List  L.remove()  

Output:

Original List
After Insertion
[1, 2, 3, 4, 5]
Traceback (most recent call last):
File "/home/c90487eefa7474c0566435269f50a52a.py", line 33, in <module>
L.remove()
File "/home/c90487eefa7474c0566435269f50a52a.py", line 15, in remove
raise RuntimeError("Deletion not allowed")
RuntimeError: Deletion not allowed

Note: For more information, refer UserList in Python

UserString

UserString is a string like container and just like UserDict and UserList it acts as a wrapper around string objects. It is used when someone wants to create their own strings with some modified or additional functionality. 

Syntax:

class collections.UserString(seq)

Example:

Python
# Python program to demonstrate  # userstring         from collections import UserString         # Creating a Mutable String  class Mystring(UserString):             # Function to append to      # string      def append(self, s):          self.data += s                 # Function to remove from       # string      def remove(self, s):          self.data = self.data.replace(s, "")         # Driver's code  s1 = Mystring("Geeks")  print("Original String:", s1.data)     # Appending to string  s1.append("s")  print("String After Appending:", s1.data)     # Removing from string  s1.remove("e")  print("String after Removing:", s1.data) 

Output:

Original String: Geeks
String After Appending: Geekss
String after Removing: Gkss

Note: For more information, refer UserString in Python



Next Article
Namedtuple in Python

N

nikhilaggarwal3
Improve
Article Tags :
  • Python
  • Python collections-module
Practice Tags :
  • python

Similar Reads

  • Python Collections Module
    The collection Module in Python provides different types of containers. A Container is an object that is used to store different objects and provide a way to access the contained objects and iterate over them. Some of the built-in containers are Tuple, List, Dictionary, etc. In this article, we will
    13 min read
  • Namedtuple in Python
    Python supports a type of container dictionary called "namedtuple()" present in the module "collections". In this article, we are going to see how to Create a NameTuple and operations on NamedTuple. What is NamedTuple in Python?In Python, NamedTuple is present inside the collections module. It provi
    8 min read
  • Deque in Python
    A deque stands for Double-Ended Queue. It is a data structure that allows adding and removing elements from both ends efficiently. Unlike regular queues, which are typically operated on using FIFO (First In, First Out) principles, a deque supports both FIFO and LIFO (Last In, First Out) operations.
    6 min read
  • ChainMap in Python
    Python contains a container called "ChainMap" which encapsulates many dictionaries into one unit. ChainMap is member of module "collections". Example: # Python program to demonstrate # ChainMap from collections import ChainMap d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} d3 = {'e': 5, 'f': 6} # Defin
    3 min read
  • Python | Counter Objects | elements()
    Counter class is a special type of object data-set provided with the collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Python's general-purpose built-ins like dictionaries, lists, and tuples. Counter is a sub-c
    6 min read
  • OrderedDict in Python
    An OrderedDict is a dictionary subclass that remembers the order in which keys were first inserted. The only difference between dict() and OrderedDict() lies in their handling of key order in Python. OrderedDict vs dict in PythonNote: Starting from Python 3.7, regular dictionaries (dict) also mainta
    10 min read
  • Defaultdict in Python
    In Python, defaultdict is a subclass of the built-in dict class from the collections module. It is used to provide a default value for a nonexistent key in the dictionary, eliminating the need for checking if the key exists before using it. Key Features of defaultdict:When we access a key that doesn
    6 min read
  • Collections.UserDict in Python
    An unordered collection of data values that are used to store data values like a map is known as Dictionary in Python. Unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Note: For mo
    2 min read
  • Collections.UserList in Python
    Python Lists are array-like data structure but unlike it can be homogeneous. A single list may contain DataTypes like Integers, Strings, as well as Objects. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a
    2 min read
  • Collections.UserString in Python
    Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. Example: C/C++ Code # Python program to demonstrate # string # Creating a String # with single Quotes String1 = 'Welcome to the Geeks Worl
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences