Modifying a List in Python: Why does one list update, but not another one "referencing" it?

Hi Everyone!

This question relates to the Python: Introduction to Lists lesson project “Gradebook” in the CS101 Career Path.

Here is what I was trying to do:

  • We have a list with 4 different subjects
  • We have a list with 4 different grades
  • Grades and subjects are combined in a joint-list called gradebook
  • I add Computer Science and its grade to all three lists
  • I add Visual Arts and its grade to all three lists
  • I change two of the grades in the grades list

My individual steps are explained in the added comments. Everything seems to work just fine, until the last step where I want to change the value of two grades.

Question: Why does the final change to the grades list update the grades list, but not the gradebook list - even though the gradebook list references the grades list? And how would I need to proceed in my code to make the adjustment of the two grades?

# Creation of the original lists
subjects = ["physics", "calculus", "poetry", "history"]
grades = [98, 97, 85, 88]
gradebook = [[subjects[0], grades[0]], [subjects[1], grades[1]], [subjects[2], grades[2]], [subjects[3], grades[3]]]

# Adding Computer Science and its grade to all lists
subjects.append("computer science")
grades.append(100)
gradebook.append([subjects[4], grades[4]])

# Adding Visual Arts and its grade to all lists
subjects.append("visual arts")
grades.append(93)
gradebook.append([subjects[5], grades[5]])

# Trying to change the value of two grades
grades[-1] = 98
grades[2] = "Pass"

print(grades)
print(gradebook)

# Print Grades: [98, 97, 'Pass', 88, 100, 98]
# Print Gradebook: [['physics', 98], ['calculus', 97], ['poetry', 85], ['history', 88], ['computer science', 100], ['visual arts', 93]]

I would’ve expected the result of print(gradebook) to look something like that:

['physics', 98], ['calculus', 97], ['poetry', 'Pass'], ['history', 88], ['computer science', 100], ['visual arts', 98]]

First time posting a question on this Forum. Although I know Discourse from another community I moderate, I’m still trying to get familiar with the layout and the rules around here. Please let me know if this is in the wrong place or if I should pay more attention to something else.

I appreciate any help, have a nice week!
Marc

1 Like

When you update gradebook with elements from grades and subjects e.g. gradebook.append([subjects[4], grades[4]]) you make new references to the objects those other references point to. At that point gradebook has nothing to do with the other lists, grades or subjects; you could delete both these lists and gradebook would be unaffected.

In this way you can change the object grades[2] references, e.g. grades[2] = "Pass" without affecting gradebook at all.

Sorry if that’s not 100% clear, in a slight rush.

1 Like

I see; so it all depends exactly on the order in which I place my code. That’s very helpful to know. I moved the lines around now and it works.

# Subjects
subjects = ["physics", "calculus", "poetry", "history"]

subjects.append("computer science")
subjects.append("visual arts")

# Grades
grades = [98, 97, 85, 88]

grades.append(100)
grades.append(93)

grades[-1] = 98
grades[2] = "Pass"

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

gradebook.append([subjects[4], grades[4]])
gradebook.append([subjects[5], grades[5]])

print(gradebook)

# Output: [['physics', 98], ['calculus', 97], ['poetry', 'Pass'], ['history', 88], ['computer science', 100], ['visual arts', 98]]

Thank you very much for your explanation!

1 Like