0

I need my variable monthS to stay as I declared it and I created a new list monthS1 where I want to remove the last value inside a while loop. It all works except for my initial variable changing alongside monthS1.

>>> monthS = ["AUG23","SEP23","OCT23","NOV23","DEC23"]
>>> monthS1 = monthS
>>> monthS1.pop()
>>> monthS1
["AUG23","SEP23","OCT23","NOV23"]

>>> monthS
["AUG23","SEP23","OCT23","NOV23"]

I tried using remove and got the same results.

  • 1
    You're not making a copy of the list, both variables refer to the same list, which you modify in place with `pop()`. – Barmar Feb 28 '23 at 16:21
  • adding to what @Barmar advises, try : `monthS1 = monthS.copy()` – JonSG Feb 28 '23 at 16:28

0 Answers0