Collections.UserDict in Python Last Updated : 01 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 more information, refer to Python Dictionary Collections.UserDict Python supports a dictionary like a container called UserDict present in the collections module. This class acts as a wrapper class around the dictionary objects. This class is useful when one wants to create a dictionary of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors to the dictionary. This class takes a dictionary instance as an argument and simulates a dictionary that is kept in a regular dictionary. The dictionary is accessible by the data attribute of this class. Syntax: collections.UserDict([initialdata]) Example 1: Python3 # Python program to demonstrate # userdict from collections import UserDict d = {'a':1, 'b': 2, 'c': 3} # Creating an UserDict userD = UserDict(d) print(userD.data) # Creating an empty UserDict userD = UserDict() print(userD.data) Output: {'a': 1, 'b': 2, 'c': 3} {} Example 2: Let's create a class inheriting from UserDict to implement a customized dictionary. Python3 # 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}) print("Original Dictionary") print(d) d.pop(1) Output: Original Dictionary {'a': 1, 'c': 3, 'b': 2}Traceback (most recent call last): File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 35, in d.pop(1) File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 20, in pop raise RuntimeError("Deletion not allowed") RuntimeError: Deletion not allowed Exception ignored in: Traceback (most recent call last): File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 15, in __del__ RuntimeError: Deletion not allowed Comment More infoAdvertise with us Next Article Collections.UserList in Python N nikhilaggarwal3 Follow 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 provid 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.E 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: Python3 # Python program to demonstrate # ChainMap from collections import ChainMap d1 = {'a': 1, 'b': 2} d2 = {'c': 3, 'd': 4} d3 = {'e': 5, 'f': 6} 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 OrderedDict is a subclass of Python's built-in dictionary dict that remembers the order in which keys are inserted. Unlike older versions of Python where dictionaries did not guarantee order, OrderedDict preserves insertion order reliably.Note: From Python 3.7 onwards, the built-in dict also preserv 7 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: Python3 # Python program to demonstrate # string # Creating a String # with single Quotes String1 = 'Welcome to the Geeks World' 2 min read Like