Stuck in Python Gradebook excercise

I’m on the excercise where I need to add a list pair within the nested list. I tried a few examples and even utilized the hint example.

All of them lead to the same error within the console:
Traceback (most recent call last):
File “script.py”, line 12, in
gradebook.append((“visual arts”, 93))
AttributeError: ‘zip’ object has no attribute ‘append’

Here’s my code:
last_semester_gradebook = [(“politics”, 80), (“latin”, 96), (“dance”, 97), (“architecture”, 65)]
subjects = [“physics”,“calculus”,“poetry”,“history”]
grades = [98,97,85,88]
subjects.append(“computer science”)
grades.append(100)

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

#Next line leads to an error
gradebook.append((“visual arts”, 93))

print(list(gradebook))

Resolved! This was my workaround.

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

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

grades = [98,97,85,88]

subjects.append(“computer science”)

grades.append(100)

gradebook = zip(subjects,grades)

#print(list(gradebook))

gradebook = list(gradebook) + [(“visual arts”, 93)]

print(list(gradebook))

The error is occurring because gradebook is not a list. When you print list(gradebook), you are printing an explicit list of tuples. zip returns an iterator, not a list. If you had assigned

gradebook = list(zip(subjects,grades))

you could have appended to gradebook.

4 Likes

It’s worth pointing out that zip used to return a list in Python2 which might have caused confusion-
https://docs.python.org/2.7/library/functions.html#zip

.append would work as expected in that case since gradebook would be a list anyway.

1 Like