Not only can we create our own version of a dictionary, the UserList wrapper container lets us create our own list as well! This class contains all of the functionality of a regular list, but it also has a property called data which allows us to access the list contents directly. Here is an example of a modified list using the container wrapper:

from collections import UserList # Create a class which inherits from the UserList class class CondenseList(UserList): # A new method to remove duplicate items from the list def condense(self): self.data = list(set(self.data)) print(self.data) # We can also overwrite a method from the list class def clear(self): print("Deleting all items from the list!") super().clear() condense_list = CondenseList(['t-shirt', 'jeans', 'jeans', 't-shirt', 'shoes']) condense_list.condense() condense_list.clear()

As shown in this code example, we can add additional methods and overwrite methods from the UserList class. This is the same as inheriting from regular classes in Python.

Let’s try creating our own list class!



Now, let’s try creating a custom list class using UserList. Create a new class called ListSorter which inherits from the UserList class. Inside of this class, overwrite the .append() method to sort the list after appending the value to it.


Now that we have created our own list class, try creating an object using it’s constructor. Create an object called sorted_list and pass data into the ListSorter constructor. Afterwards, append the value 2 to the new object and print out the results.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?