I have some code that does some manipulation of classes. The details aren’t terribly important. headerDict is a dictionary that contains lists of objects from the class NodeCluster. Node1 and Node2 are objects from the class NodeCluster. .header is an attribute of the NodeCluster class. What I want to do is iterate over the class objects in the node2 entry of headerDict, change their header attributes, and — here’s the problem part — append each class object to a list in node1’s headerDict. Here’s a code snippet:
if len(headerDict[node1.header]) >= len(headerDict[node2.header]):
for node in headerDict[node2.header]:
node.k = node.k - 1
node.header = node1.header
headerDict[node1].append(node)
However, when I try to perform the append operation, I get this error:
KeyError: <__main__.NodeCluster instance at 0x10047b050>
What gives?
I suspect the following will fix it:
since everywhere else you’re using
.headerto index intoheaderDict.