list copy

List copy is key, Create a new list that’s free, Changes won’t touch thee.

python shell for list copy

List copy, oh list copy,
Make a new list, don’t be sloppy.
Create a copy that stands apart,
So changes in one don’t affect its counterpart.

In this Python session, we learned about list copy. We started by creating a list x with elements 1, 2, and 3. Then, we created a list y and set it equal to x. We learned that when we change a value in y, it also changes the corresponding value in x, because y is just a reference to x. To make an independent copy of x, we used the y = x.copy() method. This created a new list y that is separate from x, so changing values in y does not affect x. We also saw that using y = x[:] achieves the same result. It’s important to remember that when making a copy of a list that contains mutable elements (like another list), a shallow copy is created, so changing these mutable elements in the copied list will still affect the original list.