Skip to content
Advertisement

Why the specific index position not working with the .remove? [closed]

I’m trying to do the Gradebook python project. For some reason I’m getting value error with specific index. Other index within the array I tried is working fine except for the specific index. Can anyone tell what’s wrong with the code :

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

# list called subjects
subjects = ["physics", "calculus", "poetry", "history"]

# list called grades
grades = [98, 97, 85, 88]

# gradebooks
gradebook = [
  [subjects[0], grades[0]],
  [subjects[1], grades[1]],
  [[subjects[2], grades[2]]],
  [subjects[3], grades[3]],
]

print(gradebook)

# Adding additional grades
gradebook.append(["Computer science", 100])
gradebook.append(["Visual arts", 93])

# adding 5 points
gradebook[-1][-1] = (93 + 5)
print(gradebook)

gradebook.remove(['poetry', 85])
print(gradebook)

Other index position works except for 2nd index. Thank you in advance.

Advertisement

Answer

After reformatting your code, it becomes somewhat obvious that you’re doing something different with the second index:

gradebook = [
  [subjects[0], grades[0]],
  [subjects[1], grades[1]],
  [[subjects[2], grades[2]]],
  [subjects[3], grades[3]],
]

Presumably it should be:

gradebook = [
  [subjects[0], grades[0]],
  [subjects[1], grades[1]],
  [subjects[2], grades[2]],
  [subjects[3], grades[3]],
]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement