From bcba6d589f577faf3ac0d104f04c9680fc5bf259 Mon Sep 17 00:00:00 2001 From: Silvia Pasciullo <117067632+silviapasc@users.noreply.github.com> Date: Fri, 10 Oct 2025 11:46:43 +0200 Subject: [PATCH 1/8] Create userlist.md --- .../terms/userlist/userlist.md | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 content/python/concepts/collections-module/terms/userlist/userlist.md diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md new file mode 100644 index 00000000000..7678f087b6f --- /dev/null +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -0,0 +1,134 @@ +--- +Title: 'UserList' +Description: 'Wrapper around list objects for easier list subclassing.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Classes' + - 'Modules' + - 'Python' +CatalogContent: + - 'learn-example-course' + - 'paths/example-path' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +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 with modified behavior and/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. + +## Syntax + +```pseudo +collections.UserList(list) +``` + +**Parameters:** + +- `list`: a regular list object used to store the contents of the UserList class. The list is empty by default and can be accessible via the UserList `data` attribute. + +**Return value:** + +A `` object is returned. + +## Example 1 + +This example showcases a basic use of `UserList` as a wrapper around a list: + +```py +from collections import UserList + +# Create a regular list +l = ['USD', 'GBP', 'EUR'] +print(l) # Output: ['USD', 'GBP', 'EUR'] + +# Instantiate a UserList object from the list +ul = UserList(l) +print(ul) # Output: ['USD', 'GBP', 'EUR'] + +# Print out the data type for each instantiated object +print(type(l)) # Output: +print(type(ul)) # Output: +``` + +Through the `data` attribute it is possible to access the `ul` content and its built-in methods: + +```py +print(ul.data) # Output: ['USD', 'GBP', 'EUR'] + +# Append a new item to the UserList object +ul.data.append('$') +print(ul.data) # Output: ['USD', 'GBP', 'EUR', '$'] + +# Remove the item +ul.data.remove('$') + +# Sort the list-like object in ascending order +ul.data.sort() +print(ul.data) # Output: ['EUR', 'GBP', 'USD'] +``` + +## Example 2 + +In the following example, the `CurrencyCodeList` class is instantiated, which inherits from `UserList` and its properties. The class stores only string items from the initial iterable; if non-string items are provided, it returns an empty `UserList` object by default. Additionally, the append `method` is overridden to accept only uppercase strings of exactly three characters; if the new item does not meet this criterion, a `RuntimeError` is raised. + +```py +from collections import UserList + +class CurrencyCodeList(UserList): + def __init__(self, iterable): + # Call the parent constructor with a generator that filters input to only strings + super().__init__(item for item in iterable if isinstance(item, str)) + + def append(self, other): + # Check if the provided value is in lowercase, which is not allowed + if other == str(other.lower()): + raise RuntimeError("Lowercase not allowed") + # Check if the length of the value is exactly 3 characters, which is required + if len(other) != 3: + raise RuntimeError("3 string characters required") + else: + self.data.append(other) +``` + +The code here below shows that `currency` is created as an instance of `CurrencyCodeList`, inheriting its behavior from the class: + +```py +currency = CurrencyCodeList(['JPY', 'CHF', 'EUR']) +print(currency) # Output: ['JPY', 'CHF', 'EUR'] + +currency_wrong_type = CurrencyCodeList([000, 555, 999]) +print(currency_wrong_type) # Output: [] + +currency.append('$') # Output: RuntimeError: Lowercase not allowed +currency.append('AB') # Output: RuntimeError: 3 string characters required + +currency.append('GBP') +print(currency) # Output: ['JPY', 'CHF', 'EUR', 'GBP'] +``` + +## Codebyte Example + +In this example, a class `NoRemovedItem` is defined, inheriting from `UserList`. It initializes with an iterable and stores its items via the parent class constructor. It also overrides `remove` and `pop` methods to raise a `RuntimeError` with the message "Deletion not allowed", which effectively prevents item removal. + +```codebyte/py +from collections import UserList + +class NoRemovedItemList(UserList): + def __init__(self, iterable): + # The constructor takes an iterable of items + super().__init__(item for item in iterable) + + def remove(self, other): + raise RuntimeError("Deletion not allowed") + + def pop(self, idx=None): + raise RuntimeError("Deletion not allowed") + + +# Instantiate a NoRemovedItemList item and test its properties +ids = NoRemovedItemList([1233, 4566, 7899, 0000]) +ids.remove(0000) # Output: RuntimeError: Deletion not allowed +ids.pop(0000) # Output: RuntimeError: Deletion not allowed +``` From 2cdabffa4ad096b33252b328c4be10a430ac0e33 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 11:51:11 +0530 Subject: [PATCH 2/8] Updated headers --- .../collections-module/terms/userlist/userlist.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index 7678f087b6f..28512057414 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -16,7 +16,7 @@ CatalogContent: - 'paths/computer-science' --- -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 with modified behavior and/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. +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. ## Syntax @@ -26,13 +26,13 @@ collections.UserList(list) **Parameters:** -- `list`: a regular list object used to store the contents of the UserList class. The list is empty by default and can be accessible via the UserList `data` attribute. +- `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. **Return value:** A `` object is returned. -## Example 1 +## Example 1: Basic Usage of `collections.UserList` This example showcases a basic use of `UserList` as a wrapper around a list: @@ -69,7 +69,7 @@ ul.data.sort() print(ul.data) # Output: ['EUR', 'GBP', 'USD'] ``` -## Example 2 +## Example 2: Creating a Custom List Class with UserList In the following example, the `CurrencyCodeList` class is instantiated, which inherits from `UserList` and its properties. The class stores only string items from the initial iterable; if non-string items are provided, it returns an empty `UserList` object by default. Additionally, the append `method` is overridden to accept only uppercase strings of exactly three characters; if the new item does not meet this criterion, a `RuntimeError` is raised. From 1232ed8a1335c3454053ed1914dcda020192e2b9 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 11:52:42 +0530 Subject: [PATCH 3/8] removed codebyte due to runtime error --- .../terms/userlist/userlist.md | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index 28512057414..7cb9994b746 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -107,28 +107,3 @@ currency.append('AB') # Output: RuntimeError: 3 string characters required currency.append('GBP') print(currency) # Output: ['JPY', 'CHF', 'EUR', 'GBP'] ``` - -## Codebyte Example - -In this example, a class `NoRemovedItem` is defined, inheriting from `UserList`. It initializes with an iterable and stores its items via the parent class constructor. It also overrides `remove` and `pop` methods to raise a `RuntimeError` with the message "Deletion not allowed", which effectively prevents item removal. - -```codebyte/py -from collections import UserList - -class NoRemovedItemList(UserList): - def __init__(self, iterable): - # The constructor takes an iterable of items - super().__init__(item for item in iterable) - - def remove(self, other): - raise RuntimeError("Deletion not allowed") - - def pop(self, idx=None): - raise RuntimeError("Deletion not allowed") - - -# Instantiate a NoRemovedItemList item and test its properties -ids = NoRemovedItemList([1233, 4566, 7899, 0000]) -ids.remove(0000) # Output: RuntimeError: Deletion not allowed -ids.pop(0000) # Output: RuntimeError: Deletion not allowed -``` From 6b1d62017e2e81de6058a7e75032e2c797ff4504 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 12:00:15 +0530 Subject: [PATCH 4/8] second example removed --- .../terms/userlist/userlist.md | 66 +++++++------------ 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index 7cb9994b746..face0065c13 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -32,7 +32,7 @@ collections.UserList(list) A `` object is returned. -## Example 1: Basic Usage of `collections.UserList` +## Example: Basic Usage of `collections.UserList` This example showcases a basic use of `UserList` as a wrapper around a list: @@ -41,69 +41,47 @@ from collections import UserList # Create a regular list l = ['USD', 'GBP', 'EUR'] -print(l) # Output: ['USD', 'GBP', 'EUR'] +print(l) # Instantiate a UserList object from the list ul = UserList(l) -print(ul) # Output: ['USD', 'GBP', 'EUR'] +print(ul) # Print out the data type for each instantiated object -print(type(l)) # Output: -print(type(ul)) # Output: +print(type(l)) +print(type(ul)) ``` -Through the `data` attribute it is possible to access the `ul` content and its built-in methods: +The code returns the following output: + +```shell +['USD', 'GBP', 'EUR'] +['USD', 'GBP', 'EUR'] + + +``` + +Through the `data` attribute, it is possible to access the `ul` content and its built-in methods: ```py print(ul.data) # Output: ['USD', 'GBP', 'EUR'] # Append a new item to the UserList object ul.data.append('$') -print(ul.data) # Output: ['USD', 'GBP', 'EUR', '$'] +print(ul.data) # Remove the item ul.data.remove('$') # Sort the list-like object in ascending order ul.data.sort() -print(ul.data) # Output: ['EUR', 'GBP', 'USD'] -``` - -## Example 2: Creating a Custom List Class with UserList - -In the following example, the `CurrencyCodeList` class is instantiated, which inherits from `UserList` and its properties. The class stores only string items from the initial iterable; if non-string items are provided, it returns an empty `UserList` object by default. Additionally, the append `method` is overridden to accept only uppercase strings of exactly three characters; if the new item does not meet this criterion, a `RuntimeError` is raised. - -```py -from collections import UserList - -class CurrencyCodeList(UserList): - def __init__(self, iterable): - # Call the parent constructor with a generator that filters input to only strings - super().__init__(item for item in iterable if isinstance(item, str)) - - def append(self, other): - # Check if the provided value is in lowercase, which is not allowed - if other == str(other.lower()): - raise RuntimeError("Lowercase not allowed") - # Check if the length of the value is exactly 3 characters, which is required - if len(other) != 3: - raise RuntimeError("3 string characters required") - else: - self.data.append(other) +print(ul.data) ``` -The code here below shows that `currency` is created as an instance of `CurrencyCodeList`, inheriting its behavior from the class: - -```py -currency = CurrencyCodeList(['JPY', 'CHF', 'EUR']) -print(currency) # Output: ['JPY', 'CHF', 'EUR'] - -currency_wrong_type = CurrencyCodeList([000, 555, 999]) -print(currency_wrong_type) # Output: [] - -currency.append('$') # Output: RuntimeError: Lowercase not allowed -currency.append('AB') # Output: RuntimeError: 3 string characters required +The above codeb will return the following output: -currency.append('GBP') -print(currency) # Output: ['JPY', 'CHF', 'EUR', 'GBP'] +```shell +['USD', 'GBP', 'EUR'] +['USD', 'GBP', 'EUR', '$'] +['EUR', 'GBP', 'USD'] ``` From 02791922d4abf01722a680cc80e15f9b1d71a5e2 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 12:12:33 +0530 Subject: [PATCH 5/8] fixing check errors --- .../collections-module/terms/userlist/userlist.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index face0065c13..3f060c1568d 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -41,15 +41,15 @@ from collections import UserList # Create a regular list l = ['USD', 'GBP', 'EUR'] -print(l) +print(l) # Instantiate a UserList object from the list ul = UserList(l) -print(ul) +print(ul) # Print out the data type for each instantiated object -print(type(l)) -print(type(ul)) +print(type(l)) +print(type(ul)) ``` The code returns the following output: @@ -75,7 +75,7 @@ ul.data.remove('$') # Sort the list-like object in ascending order ul.data.sort() -print(ul.data) +print(ul.data) ``` The above codeb will return the following output: From 19c98f9bbb3fcf7c859048482cb73a8fef9e71ac Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 31 Oct 2025 12:15:15 +0530 Subject: [PATCH 6/8] remove duplicate CatalogContent --- .../concepts/collections-module/terms/userlist/userlist.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index 3f060c1568d..0979c5a7cc3 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -8,9 +8,6 @@ Tags: - 'Classes' - 'Modules' - 'Python' -CatalogContent: - - 'learn-example-course' - - 'paths/example-path' CatalogContent: - 'learn-python-3' - 'paths/computer-science' From 382cc5348ea8d7a7ba63e0ad90aaaa015b601fa5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 4 Nov 2025 11:49:44 +0530 Subject: [PATCH 7/8] Update userlist.md with codebyte example --- .../terms/userlist/userlist.md | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index 0979c5a7cc3..b0449a731ce 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -13,21 +13,21 @@ CatalogContent: - 'paths/computer-science' --- -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. +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. ## Syntax ```pseudo -collections.UserList(list) +collections.UserList([list]) ``` **Parameters:** -- `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. +- `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. **Return value:** -A `` object is returned. +Returns a `collections.UserList` instance that behaves like a standard Python list. ## Example: Basic Usage of `collections.UserList` @@ -75,10 +75,34 @@ ul.data.sort() print(ul.data) ``` -The above codeb will return the following output: +The above code will return the following output: ```shell ['USD', 'GBP', 'EUR'] ['USD', 'GBP', 'EUR', '$'] ['EUR', 'GBP', 'USD'] ``` + +## Codebyte Example: Creating a Custom List Using UserList + +The following example demonstrates how `UserList` can be subclassed to restrict unwanted behavior, here, preventing negative numbers from being added: + +```codebyte/python +from collections import UserList + +# Custom list that disallows negative numbers +class PositiveList(UserList): + def append(self, item): + if item < 0: + print("Negative values not allowed!") + else: + super().append(item) + +# Create and modify the custom UserList +pl = PositiveList([1, 3, 5]) +print("Initial list:", pl) + +pl.append(10) +pl.append(-4) # This will trigger the custom rule +print("Final list:", pl) +``` From 8fdbe617d2886373e259374a53e16d8fadc2f4c0 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 4 Nov 2025 11:51:10 +0530 Subject: [PATCH 8/8] Update userlist.md --- .../terms/userlist/userlist.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/content/python/concepts/collections-module/terms/userlist/userlist.md b/content/python/concepts/collections-module/terms/userlist/userlist.md index b0449a731ce..7994fb510f2 100644 --- a/content/python/concepts/collections-module/terms/userlist/userlist.md +++ b/content/python/concepts/collections-module/terms/userlist/userlist.md @@ -58,21 +58,21 @@ The code returns the following output: ``` -Through the `data` attribute, it is possible to access the `ul` content and its built-in methods: +The `UserList` behaves like a standard list, but its contents are stored in the `data` attribute: ```py -print(ul.data) # Output: ['USD', 'GBP', 'EUR'] +print(ul.data) # Access the underlying list -# Append a new item to the UserList object -ul.data.append('$') -print(ul.data) +# Append a new item +ul.append('$') +print(ul) -# Remove the item -ul.data.remove('$') +# Remove an item +ul.remove('$') # Sort the list-like object in ascending order -ul.data.sort() -print(ul.data) +ul.sort() +print(ul) ``` The above code will return the following output: @@ -83,7 +83,7 @@ The above code will return the following output: ['EUR', 'GBP', 'USD'] ``` -## Codebyte Example: Creating a Custom List Using UserList +## Codebyte Example: Creating a Custom List Using `UserList` The following example demonstrates how `UserList` can be subclassed to restrict unwanted behavior, here, preventing negative numbers from being added: