Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
a = [1, 2, 3] and then b = a and b.append(4). What is a? How do you make a separate copy?
b = a gives a second name for the same list, not a copy.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
After b = a, the two names point to the same list. The list is not copied. So b.append(4) changes the list that a also names, and a is now [1, 2, 3, 4].
To make a separate copy, write c = a.copy(). Then c.append(5) changes only c.
Answer: a is [1, 2, 3, 4]. Use a.copy() for a separate list.
Log in to join the discussion.
Loading…