{"id":25779,"date":"2021-12-16T09:17:53","date_gmt":"2021-12-16T03:47:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=25779"},"modified":"2021-12-16T09:17:53","modified_gmt":"2021-12-16T03:47:53","slug":"python-program-for-collections-userdict-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-collections-userdict-function\/","title":{"rendered":"Python Program for collections.UserDict() Function"},"content":{"rendered":"
Dictionary in Python:<\/strong><\/p>\n A Python dictionary is a list of objects that are not in any particular order i.e Unordered.<\/p>\n A dictionary is made up of a collection of key-value pairs. Each key-value pair corresponds to a specific value.<\/p>\n Curly braces { } can be used to describe a dictionary by enclosing a comma-separated list of key-value pairs.<\/p>\n Every key is separated from its associated value by a colon \u201c:\u201d<\/p>\n collections.UserDict() Function:<\/strong><\/p>\n Python has a dictionary-like container called UserDict, which is available in the collections module. This class serves as a container for dictionary objects. This class is useful when one wants to create their own dictionary, either with modified or new functionality. It can be thought of as a method of adding new behaviors to the dictionary. This class takes a dictionary instance as an argument and simulates the contents of a regular dictionary. This class’s data attribute provides access to the dictionary.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionary in Python: A Python dictionary is a list of objects that are not in any particular order i.e Unordered. A dictionary is made up of a collection of key-value pairs. Each key-value pair corresponds to a specific value. Curly braces { } can be used to describe a dictionary by enclosing a comma-separated list …<\/p>\ncollections.UserDict([initial data])<\/pre>\n
Python Program for collections.UserDict() Function<\/h2>\n
\n
Method #1: Using collections Module (Static Input)<\/h3>\n
\n
# Import UserDict() function from the collections module using the import keyword.\r\nfrom collections import UserDict\r\n# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {'p': 180, 'q': 190, 'r': 200}\r\n# Pass the given dictionary as an argument to the UserDict() function to\r\n# create a user dictionary for the given dictionary.\r\n# Store it in another variable.\r\nrsltuesr_dicnry = UserDict(gvn_dict)\r\n# Print the above result.\r\nprint(\"The user dictionary for the given dictionary is :\")\r\nprint(rsltuesr_dicnry.data)\r\n# Create an empty user dictionary using the UserDict() function and store\r\n# it in another variable.\r\nemty_userdictry = UserDict()\r\n# Print the above obtained empty user dictionary.\r\nprint(\"The Empty user dictionary is :\")\r\nprint(emty_userdictry.data)\r\n<\/pre>\n
The user dictionary for the given dictionary is :\r\n{'p': 180, 'q': 190, 'r': 200}\r\nThe Empty user dictionary is :\r\n{}<\/pre>\n
\n
# Import UserDict() function from the collections module using the import keyword.\r\nfrom collections import UserDict\r\n \r\n \r\n# Create a class by passing the UserDict function as an argument.\r\nclass GivenDictionary(UserDict):\r\n \r\n # Inside the class, create a function for stopping\r\n # the deletion of items from a given dictionary.\r\n def delete(self):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\n \r\n # Create another function by passing the parameter as none for stopping the\r\n # poping of items from a given dictionary.\r\n def pop(self, a = None):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\n \r\n # Create another function by passing the parameter as none for stopping the\r\n # popitem from a given dictionary.\r\n def popitem(self, a = None):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\n \r\n# Inside the main function\r\n# Give the dictionary as static input and store it in a variable.\r\ngvn_dict = {'p':4,'q':9 ,'z': 13}\r\n# Create an object for the above class by passing the given dictionary as an argument \r\n# and store it in another variable.\r\nrslt_dictry = GivenDictionary(gvn_dict)\r\n# Print the given dictionary. \r\nprint(\"The given dictionary = \")\r\nprint(rslt_dictry)\r\n# Delete an item from the dictionary using the pop() function.\r\nrslt_dictry.pop(1)\r\n<\/pre>\n
The given dictionary = \r\n{'p': 4, 'q': 9, 'z': 13}\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 36, in <module>\r\nrslt_dictry.pop(1)\r\nFile \"jdoodle.py\", line 18, in pop\r\nraise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\nRuntimeError: You Cannot delete an element from the given dictionary<\/pre>\n
Method #2: Using collections Module (User Input)<\/h3>\n
\n
# Import UserDict() function from the collections module using the import keyword.\r\nfrom collections import UserDict\r\n# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dict = dict()\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dict[keyy] = valuee\r\n\r\n# Pass the given dictionary as an argument to the UserDict() function to\r\n# create a user dictionary for the given dictionary.\r\n# Store it in another variable.\r\nrsltuesr_dicnry = UserDict(gvn_dict)\r\n# Print the above result.\r\nprint(\"The user dictionary for the given dictionary is :\")\r\nprint(rsltuesr_dicnry.data)\r\n# Create an empty user dictionary using the UserDict() function and store\r\n# it in another variable.\r\nemty_userdictry = UserDict()\r\n# Print the above obtained empty user dictionary.\r\nprint(\"The Empty user dictionary is :\")\r\nprint(emty_userdictry.data)\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 3\r\nEnter key and value separated by spaces = x welcome\r\nEnter key and value separated by spaces = y to\r\nEnter key and value separated by spaces = z Python-Programs\r\nThe user dictionary for the given dictionary is :\r\n{'x': 'welcome', 'y': 'to', 'z': 'Python-Programs'}\r\nThe Empty user dictionary is :\r\n{}<\/pre>\n
\n
# Import UserDict() function from the collections module using the import keyword.\r\nfrom collections import UserDict\r\n \r\n \r\n# Create a class by passing the UserDict function as an argument.\r\nclass GivenDictionary(UserDict):\r\n \r\n # Inside the class, create a function for stopping\r\n # the deletion of items from a given dictionary.\r\n def delete(self):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\n \r\n # Create another function by passing the parameter as none for stopping the\r\n # poping of items from a given dictionary.\r\n def pop(self, a = None):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\n \r\n # Create another function by passing the parameter as none for stopping the\r\n # popitem from a given dictionary.\r\n def popitem(self, a = None):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\n \r\n# Inside the main function\r\n# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dict = dict()\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dict[keyy] = valuee\r\n \r\n# Create an object for the above class by passing the given dictionary as an argument \r\n# and store it in another variable.\r\nrslt_dictry = GivenDictionary(gvn_dict)\r\n# Print the given dictionary. \r\nprint(\"The given dictionary = \")\r\nprint(rslt_dictry)\r\n# Delete an item from the dictionary using the pop() function.\r\nrslt_dictry.pop(1)\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 2\r\nEnter key and value separated by spaces = welcometo 12\r\nEnter key and value separated by spaces = Python-Programs 13\r\nThe given dictionary = \r\n{'welcometo': '12', 'Python-Programs': '13'}\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 49, in <module>\r\nrslt_dictry.pop(1)\r\nFile \"jdoodle.py\", line 18, in pop\r\nraise RuntimeError(\"You Cannot delete an element from the given dictionary\")\r\nRuntimeError: You Cannot delete an element from the given dictionary<\/pre>\n","protected":false},"excerpt":{"rendered":"