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 list is done with 0 being the first index.Â
Note: For more information, refer to Python List
Â
Collections.UserList
Python supports a List like a container called UserList present in the collections module. This class acts as 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 as a way of adding new behaviors for the list. This class takes a list instance as an argument and simulates a list that is kept in a regular list. The list is accessible by the data attribute of the 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 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
Â