In the code below, modification of the first type changes the original list, while in the second list stays intact. Why is the behaviour the way it is?
temp = [{"a":"b"},{"c":"d"},{"e":"f"},{"a":"c"}]
for item in temp:
if "a" in item:
item["a"] = "x"
print(temp)
temp = [{"a":"b"},{"c":"d"},{"e":"f"},{"a":"c"}]
for item in temp:
item = {}
print(temp)
Output for the first one is [{'a': 'x'}, {'c': 'd'}, {'e': 'f'}, {'a': 'x'}]
and for the second one is [{'a': 'b'}, {'c': 'd'}, {'e': 'f'}, {'a': 'c'}]
Python version is 3.6.5