Deleting an element from a 2d list -need help. Project Gradebook

[py]

[/py]
last_semester_gradebook = [[“politics”, 80], [“latin”, 96], [“dance”, 97], [“architecture”, 65]]

Your code below:

subjects = [“physics”, “calculus”, “poetry”, “history”]

grades = [98, 97, 85, 88]

gradebook = [[“physics”, 98], [“calculus”, 97], [“poetry”, 85], [“history”, 88]]

gradebook.append([“computer science”, 100])

gradebook.append([“visual arts”, 93])

gradebook[-1][1] += 5

“”"Stuck here:
gradebook = [[“physics”, 98], [“calculus”, 97], [“poetry”, 85], [“history”, 88]]

You decided to switch from a numerical grade value to a Pass/Fail option for your poetry class.

Find the grade value in your gradebook for your poetry class and use the .remove() method to delete it. “”"

gradebook.remove([2][1]) How can I remove the second element from a 2d list?

full_gradebook = gradebook + last_semester_gradebook

print (full_gradebook)

1 Like

In order to correctly interpret the instruction, we’ll need to read the narrative and see how the problem is set up. Please post a link to this exercise.

As to the mechanics of removing an element from a nested list. the same applies as would apply to a 1D list. Apply it to the nested object.

If the object is x and the index is i, then the identifier of that element is, x[i]. So if we can apply a method or function to x, the same can be applied to x[i] (the nested list).

Remove an item from a list in Python (clear, pop, remove, del) | note.nkmk.me

Here is the exercise link: https://www.codecademy.com/courses/learn-python-3/projects/python-gradebook

1 Like

I got it to work by following the same mechanics as .append.

gradebook.remove([“poetry”, 85])

Above you are removing the entire list, and not just the grade. The lesson asks us to remove the grade, only, and then append "Pass".

gradebook[2].remove(85)
gradebook[2].append("Pass")
#    x    i

Extra Study

For when you learn the list.index() method, which we can use on a longer list where we know there are no duplicate subjects…

gradebook[gradebook.index(["poetry", 85])].remove(85)
gradebook[gradebook.index(["poetry"])].append("Pass")

Overkill, in this instance, but good to know.

5 Likes

I’ve tried that and I get this error. What am I missing?

At line 17, cast the zip object to a list, then print it. That way the list stays intact. Recall that zip objects are consumed, and they are not subscriptable the way a list is.

Thanks @mtf. Sorry, I have no clue what it means to cast a zip object to a list. What lesson should I go to to find more info on that?

Casting involves invoking the constructor.

z = zip(a, b)
z = list(z)
print (z)

The second line above consumes the zip object but we have preserved the list.

Thank you. So I should say:

gradebook = zip(subjects, grades)
gradebook = list(gradebook)
print(gradebook)

?

1 Like

Or something to that effect. I reuse the variable since it now points to an empty zip object. Casting before printing means the list is preserved. If you print and cast in one go, you cannot print a second time.

1 Like