+ 2

Python (for me) odd behavior with variable copy. Explanations?

I had a code not working properly. After finding the problem I wrote a little code to demonstrate it. Can anyone explain this odd behavior of the copy when using .pop? https://code.sololearn.com/cZH4x4Zj99pd/?ref=app

30th Mar 2018, 2:45 AM
Edward
Edward - avatar
2 Answers
+ 9
You're working with the same object. sol=[1,2,3,4,5] solo=sol # same id print(id(sol), id(solo)) # Use the copy import, show documentation import copy print(copy.__doc__) # new copy, new id solo2 = copy.copy(solo) print(id(solo2))
30th Mar 2018, 2:51 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Both of them refer to the same object.
30th Mar 2018, 3:19 AM
Kaustubh Patil
Kaustubh Patil - avatar