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:

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: