Being new to Python, I am studying the following codes:
a_0 = {"what": "b"}
a_1 = {"what": "c"}
a_2 = {"what": "d"}
items = [] # empty list
for i_no in range(10): # fill the list with 10 identical a_0 dicts
i_new = dict(a_0)
items.append(i_new)
for i in items[0:5]: # change the first 5 items
if i["what"] == "b": # try to change the 'i' to be a_1 dicts
i = dict(a_1)
# print(i) shows changes
for i in items:
print(i)
# print(i) does not show changes
If the above change works then first 5 items should be the same as a_1, while the last 5 unchanged. But the printed outcome did not match my expectation and this confused me. I want to know whether I missed something. Are there any more convenient ways to change each dictionary in the list? Many thanks.