'int' object has no attribute 'remove'

Hello pythoneers

During the “Lists” lesson i didn’t see any example on how to use the .remove method on 2D lists that mix strings and integers

At the gradebook project now there’s a task to:

so i used:
gradebook[2][1].remove(85)

But then i get the error:

Traceback (most recent call last):
  File "script.py", line 14, in <module>
    gradebook[2][1].remove(85)
AttributeError: 'int' object has no attribute 'remove'

i guess because i cannot use the .remove method on an integer value, in this case (85)
So what method can i use then to remove an integer from a sublist?

last_semester_gradebook = [("politics", 80), ("latin", 96), ("dance", 97), ("architecture", 65)]

# Your code below: 

subjects = ["Physics", "Calculus", "Poetry", "History"]
#subjects.append("Computer Science")
grades = [98, 97, 85, 88]
#grades.append(100)
gradebook = [['Physics', 98], ['Calculus', 97], ['Poetry', 85], ['History', 88]]

gradebook.append(["Computer Science", 100])
gradebook.append(["Visual Arts", 93])
gradebook[-1][1] = 98
gradebook[2][1].remove(85)

print(gradebook)

As the error states, an integer has no remove() attribute. What value does gradebook[2][1] have? I recommend using print() on it.

Remember that .remove() has to be used on a list, so if gradebook is a list of lists, than the first index specifies a sublist and the next index will specify a variable in the sublist.

1 Like

the print gives

[['Physics', 98], ['Calculus', 97], ['Poetry', 85], ['History', 88], ['Computer Science', 100], ['Visual Arts', 98]]

So to remove grade “85” from poetry… thats index 2 , 1
which is an integer…
what method can i use then to remove an integer from a sublist?

If you print the object that you are attempting to call remove(85) on, you’ll see that it isn’t a list.

print(gradebook[2][1])

You need to call the remove() method on the list that contains the value to be removed.

Hint:

The list itself can be referenced like so:

gradebook[2]
# if you print it, you'll see the list containing the integer value you are attempting to remove:
print(gradebook[2])
# ['Poetry', 85]
2 Likes

i’ll retreat now in silence…ashamed :smiley:

thanks!

1 Like

No need to feel ashamed. Accessing nested elements can be confusing.

1 Like

What if i use remove() for a value that is contained several times within a list, would it remove all the entries with the same value? or would i have to call the remove method for that same value as many times as needed?

From the docs:

list. remove ( x )

Remove the first item from the list whose value is equal to x . It raises a ValueError if there is no such item.

2 Likes