keyerror 1 in my code

Your main problem is this line: You think you’re making a copy of the dictionary, but actually you still have just one dictionary, so operations on dicta also change aDict (and so, you remove values from adict, they also get removed from aDict, and so you get your KeyError). One solution would be (You should … Read more

Python Key Error=0 – Can’t find Dict error in code

The error you’re getting is that self.adj doesn’t already have a key 0. You’re trying to append to a list that doesn’t exist yet. Consider using a defaultdict instead, replacing this line (in __init__): with this: You’ll need to import at the top: Now rather than raise a KeyError, self.adj[0].append(edge) will create a list automatically to append to.