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 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.
Syntax:
collections.UserList(list)
Parameters:
list: This is required. It is a list to be given as input.
Program for collections.UserList in Python
Method #1: Using collections Module (Static Input)
Approach:
- Import UserList() function from the collections module using the import keyword.
- Give the list as static input and store it in a variable.
- Pass the given list as an argument to the UserList() function to create a user list for the given list.
- Store it in another variable.
- Print the above result.
- Create an empty user list using the UserList() function and store it in another variable.
- Print the above obtained empty user list.
- The Exit of the Program.
Below is the implementation:
# Import UserList() function from the collections module using the import keyword.
from collections import UserList
# Give the list as static input and store it in a variable.
gvn_lst = [35,20,60,12,9]
# Pass the given list as an argument to the UserList() function to create a
# user list for the given list.
# Store it in another variable.
rslt_userlst = UserList(gvn_lst)
# Print the above result.
print("The user list for the given list is :")
print(rslt_userlst.data)
# Create an empty user list using the UserList() function and store it in
# another variable.
empt_usrlst = UserList()
# Print the above obtained empty user list.
print("The Empty user list is :")
print(empt_usrlst.data)
Output:
The user list for the given list is : [35, 20, 60, 12, 9] The Empty user list is : []
Example2:
Approach:
- Import UserList() function from the collections module using the import keyword.
- Create a class by passing the UserList function as an argument.
- Inside the class, create a function by passing the parameter as none for stopping the deletion of items from a given list.
- Inside the function raise some random RuntimeError.
- Create another function by passing the parameter as none for stopping the poping of items from a given list.
- Inside the function raise some random RuntimeError.
- Â Inside the main function, Give the list as static input and store it in a variable.
- Create an object for the above class by passing the given list as an argument and store it in another variable.
- Print the list before Insertion.
- Insert an element to the above-obtained result list using the append() function.
- Print the list after appending an element to the given list.
- Delete an item from the list using the remove() function.
- The Exit of the Program.
Below is the implementation:
# Import UserList() function from the collections module using the import keyword.
from collections import UserList
# Create a class by passing the UserList function as an argument.
class GivenList(UserList):
# Inside the class, create a function by passing the parameter as none for stopping
# the deletion of items from a given list.
def remove(self, a=None):
# Inside the function raise some random RuntimeError.
raise RuntimeError("You Cannot delete an element from the given list")
# Create another function by passing the parameter as none for stopping the
# poping of items from a given list.
def pop(self, a=None):
# Inside the function raise some random RuntimeError.
raise RuntimeError("You Cannot delete an element from the given list")
# Inside the main function
# Give the list as static input and store it in a variable.
gvn_lst = [22, 33, 44, 55]
# Create an object for the above class by passing the given list as an argument
# and store it in another variable.
lst = GivenList(gvn_lst)
# Print the list before Insertion.
print("The list before Insertion :")
print(lst)
# Insert an element to the above-obtained result list using the append() function.
lst.append(66)
# Print the list after appending an element to the given list.
print("The list before Insertion :")
print(lst)
# Delete an item from the list using the remove() function.
lst.remove()
Output:
The list before Insertion :
[22, 33, 44, 55]
The list before Insertion :
[22, 33, 44, 55, 66]
Traceback (most recent call last):
File "jdoodle.py", line 38, in <module>
lst.remove()
File "jdoodle.py", line 13, in remove
raise RuntimeError("You Cannot delete an element from the given list")
RuntimeError: You Cannot delete an element from the given listMethod #2: Using collections Module (User Input)
Approach:
- Import UserList() function from the collections module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Pass the given list as an argument to the UserList() function to create a user list for the given list.
- Store it in another variable.
- Print the above result.
- Create an empty user list using the UserList() function and store it in another variable.
- Print the above obtained empty user list.
- The Exit of the Program.
Below is the implementation:
# Import UserList() function from the collections module using the import keyword.
from collections import UserList
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the UserList() function to create a
# user list for the given list.
# Store it in another variable.
rslt_userlst = UserList(gvn_lst)
# Print the above result.
print("The user list for the given list is :")
print(rslt_userlst.data)
# Create an empty user list using the UserList() function and store it in
# another variable.
empt_usrlst = UserList()
# Print the above obtained empty user list.
print("The Empty user list is :")
print(empt_usrlst.data)
Output:
Enter some random List Elements separated by spaces = 8 4 2 5 1 The user list for the given list is : [8, 4, 2, 5, 1] The Empty user list is : []
Example 2:
Approach:
- Import UserList() function from the collections module using the import keyword.
- Create a class by passing the UserList function as an argument.
- Inside the class, create a function by passing the parameter as none for stopping the deletion of items from a given list.
- Inside the function raise some random RuntimeError.
- Create another function by passing the parameter as none for stopping the poping of items from a given list.
- Inside the function raise some random RuntimeError.
- Â Inside the main function,
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Create an object for the above class by passing the given list as an argument and store it in another variable.
- Print the list before Insertion.
- Insert an element to the above-obtained result list using the append() function.
- Print the list after appending an element to the given list.
- Delete an item from the list using the remove() function.
- The Exit of the Program.
Below is the implementation:
# Import UserList() function from the collections module using the import keyword.
from collections import UserList
# Create a class by passing the UserList function as an argument.
class GivenList(UserList):
# Inside the class, create a function by passing the parameter as none for stopping
# the deletion of items from a given list.
def remove(self, a=None):
# Inside the function raise some random RuntimeError.
raise RuntimeError("You Cannot delete an element from the given list")
# Create another function by passing the parameter as none for stopping the
# poping of items from a given list.
def pop(self, a=None):
# Inside the function raise some random RuntimeError.
raise RuntimeError("You Cannot delete an element from the given list")
# Inside the main function
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Create an object for the above class by passing the given list as an argument
# and store it in another variable.
lst = GivenList(gvn_lst)
# Print the list before Insertion.
print("The list before Insertion :")
print(lst)
# Insert an element to the above-obtained result list using the append() function.
lst.append(66)
# Print the list after appending an element to the given list.
print("The list before Insertion :")
print(lst)
# Delete an item from the list using the remove() function.
lst.remove()
Output:
Enter some random List Elements separated by spaces = 6 8 25 5
The list before Insertion :
[6, 8, 25, 5]
The list before Insertion :
[6, 8, 25, 5, 66]
Traceback (most recent call last):
File "jdoodle.py", line 41, in <module>
lst.remove()
File "jdoodle.py", line 13, in remove
raise RuntimeError("You Cannot delete an element from the given list")
RuntimeError: You Cannot delete an element from the given list