0

I have some sample code right here:


test = ['a','b','c','d','e','f','g','h','i','j','k','l','m']
remove = ['a','m','g']

for a in remove:
    b = test
    b.remove(a)
    print(len(b),len(test))

which has the output:

12 12
11 11
10 10

why does it also get removed from test if I am specifying it to be removed from b? Is there a way to unlink this so I am only removing it from b but not test?

Herpadurka
  • 29
  • 1
  • a and b are pointing to the same list, they're one and the same. Just two different names for the same object. – Óscar López Sep 28 '20 at 20:21
  • Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – Wups Sep 28 '20 at 20:21
  • After the assignment, accessing b is accessing test. You should use a copy instead – CIsForCookies Sep 28 '20 at 20:23

0 Answers0