Gradebook

For the gradebook project ( https://www.codecademy.com/courses/learn-python-3/projects/python-gradebook )

Step 3 asks you to creade a 2d list that combines two lists. Up to that point, we were only taught to .append , so when you combine list1 + list2 you get one long flat list. I tried creating my own method that used a for loop, and that didnt work. Then I used zip and made some progress.

gradebook=list(zip(subjects,grades))

Now I noticed that there are parenthesis around my original subjects list instead of brackets. The problem I believe is that I can not iterate through that original list and ‘poetry’, 85 has become immutable or a tuple or w/e…

print(gradebook)
[(‘physics’, 98), (‘calculus’, 97), (‘poetry’, 85), (‘history’, 88), [‘computer science’, 100], [‘visual arts’, 98]]

Go through the gradebook project since it apparently has been changed multiple times and not QA’d and discuss the right way to merge lists to create a 2d list that can be iterated through.

subjects= ["physics","calculus","poetry","history"] grades= [98,97,85,88] gradebook=list(zip(subjects,grades)) gradebook.append(["computer science", 100]) gradebook.append(["visual arts", 93]) gradebook[-1][-1]=gradebook[-1][-1]+5 #how do i remove the value of 85 from poetry and append "pass" #gradebook[2].remove([1]) does not work #gradebook[2].remove(85) does not work print(gradebook)

Welcome to the forums.

This question has been posed many times here.
Check this out:

and this from the curriculum dev:

1 Like

Yes. zip() creates a zip object which contains immutable tuples. You’ve converted the zip object to a list. The elements of the individual tuples cannot be mutated, but lists can. I understand the project has been changed, and using zip() is no longer expected, but since you’ve come this far, if you’re up to the challenge, consider how you could replace the list elements with new tuples that contain the required changes. Since this is well beyond the scope of the project, I’ve included a possible solution below. Peek if you need/want to. Happy coding!

Code:
subjects= ["physics","calculus","poetry","history"]
grades= [98,97,85,88]

gradebook=list(zip(subjects,grades))

#append tuples rather than lists for consistency
gradebook.append(("computer science", 100))
gradebook.append(("visual arts", 93))

#assign new tuple to the element at index -1
#it contains the values from the original tuple
#with 5 added to the last element
gradebook[-1] = (gradebook[-1][0], gradebook[-1][-1] +5)

#how do i remove the value of 85 from poetry and append "pass"
gradebook[2] = (gradebook[2][0], "pass") #again, a new tuple

print(gradebook)
2 Likes

@midlindner Thanks so much! I was very close to solving it but your code and comments is a step to understanding that last part I was missing. I messed around and

I am a little confused why the tuple was able to change despite it being immutable?

1 Like

The tuple itself was not changed, but replaced with a new tuple. The tuples in this case are list elements. The element at the specified index was replaced with a new element.