0

I have a simple code here and I am unsure as to what it is doing, please care to explain. :)

data = [5,6,7,8]

useList = data

useList.pop()
print(useList)
print(data)

For some reason the output is:

[5, 6, 7]
[5, 6, 7]

I do not understand why this occurs because I do not get rid of the 'data' value, I am only wanting to get rid of the useList value.

Is there a way to fix this?

Best Regards and any help will be openly accepted. :)

1 Answers1

2

When you do useList = data you simply create another pointer to the same memory of data.

You need to create copy of data and that's done like this

useList = list(data)

Or

import copy
useList = copy.copy(data)
Hasan Ramezani
  • 5,004
  • 24
  • 30