Python 3 – Key Error = 8
lis contains the value
8 at runtime. Then you try fetch the element with the key
d3. Either of these dictionaries doesnt contain an element with the given key. Thats why the error is raised.
Your code only checks whether
lis is in
d3, but not
d2 must be the culprit. Try changing your code to:
def addD2(lis): if lis == : return if(lis in d3 and lis in d2): returnDic[lis] = insert2(d2[lis],d3[lis]) d3.pop(lis) elif lis in d2: returnDic[lis] = d2[lis]
Note: Since Im not sure what the intended logic of the whole algorithm is, it might not give you the result you expect. But it should avoid the exception.