My original Python list changes when I only modified the copy Concepts
I did new = old and then appended to new, and old grew too. Then I read about copy() so I used new = old.copy() and that worked for a flat list, but my actual data is a list of dictionaries and modifying a dictionary inside the copy still changes the original. What is going on and what is the correct way to copy this?
@venv_val · 2mo ago · 3 replies
Two separate ideas are colliding here.
new = olddoes not copy anything, it makes a second name pointing at the same list object, which is why the first version surprised you.old.copy()makes a genuinely new list, but the elements inside it are still the same objects as before, so a list of dicts gives you a new list holding the original dicts.For your case:
That recursively copies the nested structures. Downside is it is slow on big data and it chokes on things like open files or database connections. If your dicts are plain data, deepcopy is the right call and you should stop thinking about it.
Reply
Report
@rubberduck_rae · 2mo ago
Shallow versus deep was the missing vocabulary. deepcopy fixed it immediately.
Reply
Report
@vacuum_analyze · 2mo ago
Worth adding that
id(x)in the interpreter is the quickest way to check this yourself. Printid(old[0])andid(new[0])and the answer is right there.Reply
Report