First of all, sorry if this questions is extremely basic, I'm just starting with Python.
I'm having problems understanding how Python 3.6 creates and appends objects to lists. See the following code:
a_dict=dict()
a_list=list()
for i in range(100):
a_dict['original'] = i
a_dict['multi'] = i*2
a_list.append(a_dict)
The print of the list goes like
print(a_list)
>>[{'original': 99, 'multi': 198}{'original': 99, 'multi': 198}...{'original': 99, 'multi': 198}]
According to my original thoughts, i=0 -> original=0, multi=0; i=1 -> original=1, multi=2; etc...
But, according to this question here, Python's append() appends a pointer to the object not the actual value. So I change the append(original) on my original code to append(copy):
a_dict=dict()
a_list=list()
for i in range(100):
a_dict['original'] = i
a_dict['multi'] = i*2
a_list.append(a_dict.copy()) ##change here
Now, I get the desired result:
print(a_list)
[{'original': 0, 'multi': 0}, {'original': 1, 'multi': 2}, {'original': 2, 'multi': 4},...]
Now, here is my question:
How append() really works? Do always lists contain pointers-like objects to their originals? How about other types? Should I always use copy() if my intentions are not to directly mess with the original values or the list/container I'm using?
Hope I'm explaining myself good enough. And again sorry if it's a basic question. Thanks.