I am copying a dict and removing element from it. But this is affecting the original dict:
ugraph = {0: {1}, 1: {0, 2}, 2: {1, 3}, 3: {2}}
print(ugraph)
graph = ugraph.copy()
graph[0].remove(1)
print(ugraph)
gives the output:
{0: {1}, 1: {0, 2}, 2: {1, 3}, 3: {2}}
{0: set(), 1: {0, 2}, 2: {1, 3}, 3: {2}}
>Solution :
Hello you need to use deepcopy in that case.
from copy import deepcopy
...
graph = deepcopy(ugraph)
copy doesn’t work here because you have a dict of mutable object(in your case dictionaries).