Skip to content

Commit 26cac31

Browse files
authored
[Term Entry] Python Collections-module: UserList #7536 (#7717)
* Create userlist.md * Updated headers * removed codebyte due to runtime error * second example removed * fixing check errors * remove duplicate CatalogContent * Update userlist.md with codebyte example * Update userlist.md ---------
1 parent 5f29bd2 commit 26cac31

File tree

1 file changed

+108
-0
lines changed
  • content/python/concepts/collections-module/terms/userlist

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
Title: 'UserList'
3+
Description: 'Wrapper around list objects for easier list subclassing.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Classes'
9+
- 'Modules'
10+
- 'Python'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`UserList`** class from the `collections` module acts as a wrapper around the built-in [`list`](https://www.codecademy.com/resources/docs/python/lists) type, to create custom list-like [classes](https://www.codecademy.com/resources/docs/python/classes) with modified behavior or new functionalities. Although directly subclassing Python’s list reduces the need for this class, UserList remains available in the standard library for convenience and backward compatibility.
17+
18+
## Syntax
19+
20+
```pseudo
21+
collections.UserList([list])
22+
```
23+
24+
**Parameters:**
25+
26+
- `list`: A regular list object used to store the contents of the UserList class. The list is empty by default and can be accessed via the UserList `data` attribute.
27+
28+
**Return value:**
29+
30+
Returns a `collections.UserList` instance that behaves like a standard Python list.
31+
32+
## Example: Basic Usage of `collections.UserList`
33+
34+
This example showcases a basic use of `UserList` as a wrapper around a list:
35+
36+
```py
37+
from collections import UserList
38+
39+
# Create a regular list
40+
l = ['USD', 'GBP', 'EUR']
41+
print(l)
42+
43+
# Instantiate a UserList object from the list
44+
ul = UserList(l)
45+
print(ul)
46+
47+
# Print out the data type for each instantiated object
48+
print(type(l))
49+
print(type(ul))
50+
```
51+
52+
The code returns the following output:
53+
54+
```shell
55+
['USD', 'GBP', 'EUR']
56+
['USD', 'GBP', 'EUR']
57+
<class 'list'>
58+
<class 'collections.UserList'>
59+
```
60+
61+
The `UserList` behaves like a standard list, but its contents are stored in the `data` attribute:
62+
63+
```py
64+
print(ul.data) # Access the underlying list
65+
66+
# Append a new item
67+
ul.append('$')
68+
print(ul)
69+
70+
# Remove an item
71+
ul.remove('$')
72+
73+
# Sort the list-like object in ascending order
74+
ul.sort()
75+
print(ul)
76+
```
77+
78+
The above code will return the following output:
79+
80+
```shell
81+
['USD', 'GBP', 'EUR']
82+
['USD', 'GBP', 'EUR', '$']
83+
['EUR', 'GBP', 'USD']
84+
```
85+
86+
## Codebyte Example: Creating a Custom List Using `UserList`
87+
88+
The following example demonstrates how `UserList` can be subclassed to restrict unwanted behavior, here, preventing negative numbers from being added:
89+
90+
```codebyte/python
91+
from collections import UserList
92+
93+
# Custom list that disallows negative numbers
94+
class PositiveList(UserList):
95+
def append(self, item):
96+
if item < 0:
97+
print("Negative values not allowed!")
98+
else:
99+
super().append(item)
100+
101+
# Create and modify the custom UserList
102+
pl = PositiveList([1, 3, 5])
103+
print("Initial list:", pl)
104+
105+
pl.append(10)
106+
pl.append(-4) # This will trigger the custom rule
107+
print("Final list:", pl)
108+
```

0 commit comments

Comments
 (0)