{"id":25814,"date":"2021-12-16T09:18:30","date_gmt":"2021-12-16T03:48:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=25814"},"modified":"2021-12-16T09:18:30","modified_gmt":"2021-12-16T03:48:30","slug":"python-program-for-collections-userlist","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-collections-userlist\/","title":{"rendered":"Python Program for collections.UserList"},"content":{"rendered":"
Python collections.UserList():<\/strong><\/p>\n Python has a List-like container called UserList, which is found in the collections module. This class acts as a container for List objects. This class is useful when one wants to create their own list with modified or new functionality. It can be viewed as a method of adding new behaviors to the list. This class takes a list instance as an argument and simulates a regular list. The data attribute of this class provides access to the list.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n list:<\/strong> This is required. It is a list to be given as input.<\/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 Example 2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python collections.UserList(): Python has a List-like container called UserList, which is found in the collections module. This class acts as a container for List objects. This class is useful when one wants to create their own list with modified or new functionality. It can be viewed as a method of adding new behaviors to the …<\/p>\ncollections.UserList(list)<\/pre>\n
Program for collections.UserList in Python<\/h2>\n
\n
Method #1: Using collections Module (Static Input)<\/h3>\n
\n
# Import UserList() function from the collections module using the import keyword.\r\nfrom collections import UserList\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [35,20,60,12,9]\r\n# Pass the given list as an argument to the UserList() function to create a\r\n# user list for the given list.\r\n# Store it in another variable.\r\nrslt_userlst = UserList(gvn_lst)\r\n# Print the above result.\r\nprint(\"The user list for the given list is :\")\r\nprint(rslt_userlst.data)\r\n# Create an empty user list using the UserList() function and store it in\r\n# another variable.\r\nempt_usrlst = UserList()\r\n# Print the above obtained empty user list.\r\nprint(\"The Empty user list is :\")\r\nprint(empt_usrlst.data)\r\n<\/pre>\n
The user list for the given list is :\r\n[35, 20, 60, 12, 9]\r\nThe Empty user list is :\r\n[]<\/pre>\n
\n
# Import UserList() function from the collections module using the import keyword.\r\n\r\nfrom collections import UserList\r\n\r\n\r\n# Create a class by passing the UserList function as an argument.\r\nclass GivenList(UserList):\r\n\r\n # Inside the class, create a function by passing the parameter as none for stopping\r\n # the deletion of items from a given list.\r\n def remove(self, a=None):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given list\")\r\n\r\n # Create another function by passing the parameter as none for stopping the\r\n # poping of items from a given list.\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 list\")\r\n\r\n\r\n# Inside the main function\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [22, 33, 44, 55]\r\n# Create an object for the above class by passing the given list as an argument\r\n# and store it in another variable.\r\nlst = GivenList(gvn_lst)\r\n# Print the list before Insertion.\r\nprint(\"The list before Insertion :\")\r\nprint(lst)\r\n# Insert an element to the above-obtained result list using the append() function.\r\nlst.append(66)\r\n# Print the list after appending an element to the given list.\r\nprint(\"The list before Insertion :\")\r\nprint(lst)\r\n\r\n# Delete an item from the list using the remove() function.\r\nlst.remove()\r\n<\/pre>\n
The list before Insertion :\r\n[22, 33, 44, 55]\r\nThe list before Insertion :\r\n[22, 33, 44, 55, 66]\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 38, in <module>\r\nlst.remove()\r\nFile \"jdoodle.py\", line 13, in remove\r\nraise RuntimeError(\"You Cannot delete an element from the given list\")\r\nRuntimeError: You Cannot delete an element from the given list<\/pre>\n
Method #2: Using collections Module (User Input)<\/h3>\n
\n
# Import UserList() function from the collections module using the import keyword.\r\nfrom collections import UserList\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Pass the given list as an argument to the UserList() function to create a\r\n# user list for the given list.\r\n# Store it in another variable.\r\nrslt_userlst = UserList(gvn_lst)\r\n# Print the above result.\r\nprint(\"The user list for the given list is :\")\r\nprint(rslt_userlst.data)\r\n# Create an empty user list using the UserList() function and store it in\r\n# another variable.\r\nempt_usrlst = UserList()\r\n# Print the above obtained empty user list.\r\nprint(\"The Empty user list is :\")\r\nprint(empt_usrlst.data)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 8 4 2 5 1\r\nThe user list for the given list is :\r\n[8, 4, 2, 5, 1]\r\nThe Empty user list is :\r\n[]<\/pre>\n
\n
# Import UserList() function from the collections module using the import keyword.\r\n\r\nfrom collections import UserList\r\n\r\n\r\n# Create a class by passing the UserList function as an argument.\r\nclass GivenList(UserList):\r\n\r\n # Inside the class, create a function by passing the parameter as none for stopping\r\n # the deletion of items from a given list.\r\n def remove(self, a=None):\r\n # Inside the function raise some random RuntimeError.\r\n raise RuntimeError(\"You Cannot delete an element from the given list\")\r\n\r\n # Create another function by passing the parameter as none for stopping the\r\n # poping of items from a given list.\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 list\")\r\n\r\n\r\n# Inside the main function\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Create an object for the above class by passing the given list as an argument\r\n# and store it in another variable.\r\nlst = GivenList(gvn_lst)\r\n# Print the list before Insertion.\r\nprint(\"The list before Insertion :\")\r\nprint(lst)\r\n# Insert an element to the above-obtained result list using the append() function.\r\nlst.append(66)\r\n# Print the list after appending an element to the given list.\r\nprint(\"The list before Insertion :\")\r\nprint(lst)\r\n\r\n# Delete an item from the list using the remove() function.\r\nlst.remove()\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 6 8 25 5\r\nThe list before Insertion :\r\n[6, 8, 25, 5]\r\nThe list before Insertion :\r\n[6, 8, 25, 5, 66]\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 41, in <module>\r\nlst.remove()\r\nFile \"jdoodle.py\", line 13, in remove\r\nraise RuntimeError(\"You Cannot delete an element from the given list\")\r\nRuntimeError: You Cannot delete an element from the given list<\/pre>\n