Python Gradebook Project, solutions

I was stuck for awhile on the Python (LISTS) Gradebook Project and I found 2 solutions.
At first, I did this code incorrectly and got an error when trying to “append” to the zip.
I think a quick turnaround and I did this code Solution 1:

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)]
full_gradebook = list(gradebook) + last_semester_gradebook
print(full_gradebook)

Then, after some research, I did Solution 2, which is probably better. Because I misread on task #3 " Use the zip() function to combine subjects and grades . Save this zip object as a list into a variable called gradebook ."
Like I did on Solution 1, line 6, just added “gradebook = zip(subjects, grades)” and did not save it as a list - as they asked.
So, I think this is the better code here:

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 = list(zip(subjects, grades))
print(list(gradebook))
gradebook.append(['visual arts', 93])
full_gradebook = gradebook + last_semester_gradebook
print(full_gradebook)

I cannot fully understand and remember all the full Python syntax and rules as I am trying to make sense of all this, I guess in time and practice!
So if you have any input and feedback, welcome. :slight_smile:

1 Like

I think it’s often a source of confusion as zip used to return a list in Python2 whereas Python3 returns a zip object that can be iterated through, calling list with the output as an argument consumes that iterator to populate a list.

In fact solution1 may not behave the way you expect. Namely this section-

gradebook = zip(subjects, grades)
print(list(gradebook))  # consumes the zip object
# Use the now empty zip object again...
gradebook = list(gradebook) + [("visual arts", 93)]
# Have a look at gradebook at this point
print(gradebook)
2 Likes

Thank you.
So, in this case, am I right to assume that when I use this code:

gradebook = list(zip(subjects, grades))

it means - create a list named gradebook that contains a zip of 2 other lists (subjects and grades)?

1 Like

Yeah, that’s pretty much it but I’d be rather say that it would contain the items from the created zip object (not the zip itself). The zip creates an iterator first, this is then passed as the argument to list which creates a single element for each item returned from the iterator, the tuples like ("visual arts", 93).

This works because list can take an iterable as an argument effectively unpacking it item by item- Built-in Functions — Python 3.9.2 documentation

This would be different than say using a list literal [zip(subjects, grades)] which would be a list with a single element, the zip object itself, not the items contained within the zip object. A bit picky perhaps but as the list literal example shows, they’re not quite the same so the terms used should probably be a little different. Hopefully that makes sense (if not by all means say so :joy:), iterators are worth reading up on at some point since they’re used constantly in Python.

1 Like

Thanks so much for the time to explain this. At this point I am still tiny bit confused, BUT I plan to do a lot of practice and with time I am sure it will fall on its place. I just signed for 1 year subscription :smiley:

1 Like

I have a question about .remove(). Because I tried to use remove 85 in the gradebook list of question 8, but it doesn’t work for me. Here is the question and code i did:

Question: 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.

Code:
gradebook[2].remove(85) = “Pass”
print(gradebook)

Is anyone could help me with that? Thank you so much! :smiley:

https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
That’s not how remove works. If you have a list lst for example containing the elements 1, 2, 3, 4 then removing 3 would be done with lst.remove(3).

lst = [1, 2, 3, 4,]
lst.remove(3)

There’s no assignment operation involved.

Be careful with the indexing too. If in your example gradebook[2] references a list (nested) then that’s fine and you remove items from the referenced list. If you’re trying to remove an item from a specific index then use del or .pop instead of .remove. The .remove method is closer to find and remove if that helps, it acts on a matching element and does not use a specific index.

In the future please consider asking a new question if it’s not directly related to the original query (or if the original query was solved several months ago), thanks :slightly_smiling_face:.

3 Likes

https://www.codecademy.com/paths/visualize-data-with-python/tracks/python-for-data-analysis-dvp/modules/python-lists-dvp/projects/python-gradebook
Thank you for answering my question. But the question 8 shows that I need to use .remove() function. And gradebook[2] is a list and I tried to remove number from [2], but it does not work. Also, the question 8 is used to first remove number and replaced by “Pass” string. Could you help me with that? Thanks! :grin:

I’m not really sure, you’re describing doing several things and so far as I’m aware all question 8 wants is the removal. I’d highly suggest clarifying your issue perhaps with some example code and setting it up as a new question- How to ask good questions (and get good answers)

I think you are having the same problem that I struggled with as you were likely following the code that used the zip function, (keep in mind I’m a beginner) from what I understand when you zip a number of lists it doesn’t allow you to access the sub-lists (grades, subjects) that created, so I put the commands above the sip function and it started working. I used lines 6 & 7 to get the desired remove and append. I ended up using insert as I was having trouble putting “pass” in a location other then the end of the list.

I’m sure you’re likely past this now but hope this helps someone in the future

https://docs.python.org/3/tutorial/datastructures.html

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

Your code below:

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

grades = [98, 97, 85, 88]

grades.remove(85)

grades.insert(2, “pass”)

subjects.append(“computer science”)

grades.append(100)

gradebook = list(zip(subjects, grades))

gradebook.append([“visual arts”, 93])

gradebook[-1][1] = 98

print(list(gradebook))

fullgradebook = gradebook + last_semester_gradebook

print(fullgradebook)

I’m not sure if the gradebook project has been updated or if i’m missing something, but there was no last_semester_gradebook variable in my project

Extra thing dont forget to do this:
subjects = [“physics”, “calclus”, “poetry”, “history”]
grades = [98, 97, 85, 88]
gradebook = [
[“physics”, 98],
[“calculus”, 97],
[“poetry”, 85],
[“history”, 88]
]
print(gradebook)
gradebook.append([“computer_science”, 100])
print(gradebook)

I believe question 8 is just to remove grade value from poetry class.
Here is what i wrote (please note i am a beginner)

1 Like

A post was split to a new topic: My finished Gradebook Python 3 Project!