Need gradebook .remove help Step 8

https://www.codecademy.com/courses/learn-python-3/projects/python-gradebook

The instructions are: " Find the grade value in your gradebook for your poetry class and use the .remove() method to delete it."

My code: gradebook.remove(gradebook[2]) ← current code, as suggested.

This removes both items inside the square brackets. It’s easy enough to replace them but the instructions seem to suggest removing only the second item - the “grade value.” I’ve searched and tried to remove, delete and pop but get errors. The suggested code has me removing the entire thing.

Could someone offer some direction here?

gradebook[2] is a list. The above line could be expected to remove the entire item from the grade book, not just the value, 85, as the instruction suggests.

Since we know that gradebook[2] is in fact a list, then we can use the .remove() method that it has to remove the value suggested.

But that removes both elements, right? I think the instructions want me to remove only one of the elements.

Yes, one element from an inside element.

a = [
    ['a', 90],
    ['b', 95],
    ['c', 85],
    ['d', 80]
]

Now consider,

>>> a.remove(a[2])
>>> a
    [['a', 90], ['b', 95], ['d', 80]]
>>>

The .remove() method is in the execution context of a, a list class object with that attribute present.

>>> hasattr(a, 'remove')
    True
>>>

Now consider,

>>> hasattr(a[1], 'remove')
    True
>>> a[1].remove(95)
>>> a
    [['a', 90], ['b'], ['d', 80]]
>>> 

This time the .remove() method is in the execution context of a[1].

Each object has its own attributes, and each method is in the context of the object. Methods are not global functions, but tied directly to the object that owns them.

3 Likes

Thank you so much. That’s what I was looking for.

1 Like