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


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) |

Here is the exercise link:

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".

#    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)

Overkill, in this instance, but good to know.