Physics I / Lists, arrays, and structured storage
Practice question · Multiple choice

After b = a and b.append(4), printing a shows the 4 as well. What has actually happened?

Hints
  1. Ask what the assignment b = a actually stores in b.
  2. Compare b = a with b = 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.

Read the lesson: Lists, arrays, and structured storage →

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.

More questions on Lists, arrays, and structured storage