Collections.UserList in Python

Python Lists are array-like data structure but unlike it can be homogeneous. A single list may contain DataTypes similar as Integers, Strings, when well when 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 list is done with 0 being the first index.
Note: For more information, refer to Python List

Collections.UserList

Python supports a List similar as a container called UserList present in the collections module. This class acts when a wrapper class around the List objects. This class is useful when one wants to create a list of their own with some modified functionality or with some new functionality. It can be considered when a way of adding new behaviors for the list. This class takes a list instance when an argument and simulates a list that is kept in a regular list. The list is accessible by the data property of this class.
Syntax:

collections.UserList([list])

Example 1:

Python3

# Python program to demonstrate
# userlist
from collections import UserList
L = [1, 2, 3, 4]
# Creating a userlist
userL = UserList(L)
print(userL.data)
# Creating empty userlist
userL = UserList()
print(userL.data)


Output:

[1, 2, 3, 4]
[]

The time complexity of this Python program is O(n), where n is the length of the input list L.

The auxiliary space has used by this program is O(n), where n is the length of the input list L.

Python3

# 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/9399c9e865a7493dce58e88571472d23.py", line 33, in 
    L.remove()
  File "/home/9399c9e865a7493dce58e88571472d23.py", line 15, in remove
    raise RuntimeError("Deletion not allowed")
RuntimeError: Deletion not allowed

Explore More

Python 10 Programs for Beginners

Program 1:Write a program to create a 2D array using NumPy. Output: Program 2: Write a program to convert a python list to a NumPy array. Output: Program 3: Write a program to create matrix of 3×3 from 11 to 30. Output: Program 4: Write a program to create a data frame to store data of candidates who appeared in

Find index of first occurrence when an unsorted array is sorted

Find index of first occurrence when an unsorted an array is sorted Given an unsorted an array and a number x, find an index of first occurrence of x when

Find sum non repeating distinct elements array

Find sum of non-repeating (distinct) elements in an array Given an integer an array with repeated elements, the task is to find the sum of all distinct elements in the