I’m trying to create a new variable that will consist of an existing dictionary so that I can change things in this new dictionary without it affecting the old one. When I try this below, which I think would be the obvious way to do this, it still seems to edit my original dictionary when I make edits to the new one.. I have been searching for info on this but can’t seem to find anything, any info is appreciated
newdictionary = olddictionary
You’re creating a reference, instead of a copy. In order to make a complete copy and leave the original untouched, you need
copy.deepcopy(). So:Just using
a = dict(b)ora = b.copy()will make a shallow copy and leave any lists in your dictionary as references to each other (so that although editing other items won’t cause problems, editing the list in one dictionary will cause changes in the other dictionary, too).