Practice question · Multiple choice
After b = a and b.append(4), printing a shows the 4 as well. What has actually happened?
Hints
- Ask what the assignment
b = aactually stores inb. - Compare
b = awithb = a.copy()and run both.
Show the answer
A. Both names refer to one list; assignment did not copy
Why
Assignment binds a name, it does not duplicate an object, so a and b are two labels on one list, and mutating through either is visible through both. This is why .copy() and list(a) exist, and why a function that mutates a list argument changes its caller’s data with no return value in sight.
Practise Lists, arrays, and structured storage
The app has 6 more questions on this lesson, and keeps your place in the course. Physics I is free to start.