0

I have a bit of a strange issue with deleting items from a list. If list b is created by copying list a wholesale, then the first item in the list is deleted, it appears to be deleted from both a and b using the below syntax!

a = [0, 1, 2, 3, 4, 5]
b = a
del b[0]
print a
print b

Can someone explain why that is please? I only want to delete the first item in the list from b, not a.

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • 4
    b is not a copy of a, b is merely a reference of a. Changing a will also affect b and vice versa. You can make a real copy with `b = a[:]` – Primusa Apr 22 '18 at 23:27
  • ah right, i never knew that. i am mainly a SAS developer, but mess around with python in my own time. i will implement that change. thanks. – gdogg371 Apr 22 '18 at 23:28

0 Answers0