Python Lists are array-like data structure but unlike it can be homogenous. 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:
# 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] []
Example 2:
# Python program to demonstrate # userlist from collections import UserList # Creating a List where # deletion is not allowed class MyList(UserList): # Function to stop deleltion # 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) # Deliting 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, inL.remove() File "/home/9399c9e865a7493dce58e88571472d23.py", line 15, in remove raise RuntimeError("Deletion not allowed") RuntimeError: Deletion not allowed
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

