+ 3
python list initialized
When the LIST is initialized in different way, after assigned a new value, they have different results. I don't know why. Can you help me? Thanks. https://code.sololearn.com/c4S6j40UKagu/?ref=app
2 Réponses
+ 4
This is because in list_a you copy the same references of the sublist.
And in list_b you initialize sublists separetely.
That is why when you assign a value to list_a sublist, all of them take this value by reference.
add this to your code to see the difference:
for _ in list_a:
print(id(_))
for _ in list_b:
print(id(_))
+ 1
Because the 3 sub-lists in list_a are referencing the same list object