0

i've met a problem when using the append function of python. if i code like this:

a={}
a[1]=[1]
b=copy(a)
b[1].append(2)
print (a)

the result will be:

{1: [1, 2]}

why? i think that using the copy function, the address of anything related to b will not influence a. but this append function is really strange! could anyone please tell me how this works? how could i append something without influencing the original address? thanks a lot!

R.Yan
  • 111
  • 1
  • 1
  • 5

1 Answers1

0

copy only does a shallow copy. The list element a[1] is still copied by reference.

Avery235
  • 4,756
  • 12
  • 49
  • 83