0

Let's assume that a = [] and b = [1,2,3,4], if I use a.append(b), when some changes happend to b, a will change too. But if I use a.append(b[:]), a will not change when b changes.

Actually I don't understand what happends behind this.

  • 1
    Does this help answer your question? [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list). The first appends a reference to `b` to `a`, while the second makes a new list which is a copy of `b` and then appends that list to `a`. – Brian61354270 Mar 12 '20 at 21:09
  • @Brian yes! thanks so much! – Seeker Blood Mar 12 '20 at 21:12
  • `b[:]` is one way to create a copy of a `list` (or other sequence-like objects, like `tuple` or `str`). You could also just say `b.copy()` and it would be equivalent. – juanpa.arrivillaga Mar 12 '20 at 21:32

0 Answers0