NumEconCopenhagen / lectures-2019

Slides and code used in the lectures
MIT License
13 stars 23 forks source link

A list changes when another list is changed #1

Open JeppeDruedahl opened 5 years ago

JeppeDruedahl commented 5 years ago

Why is the first element of list A equal to zero when I run this code?

A = [1,2,3] 
B = A 
B[0] = 0
print(A[0])
JeppeDruedahl commented 5 years ago

The variable A is a reference to the list [1,2,3]. The second line, B = A, copies this reference, not the list itself. A and B therefore now refer to the same list.