Python 3 – Key Error = 8

Python 3 – Key Error = 8

Obviously, lis[0] contains the value 8 at runtime. Then you try fetch the element with the key 8 in d2 and 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[0] is in d3, but not d2. So d2 must be the culprit. Try changing your code to:

def addD2(lis):
    if lis == []:
        return
    if(lis[0] in d3 and lis[0] in d2):
        returnDic[lis[0]] = insert2(d2[lis[0]],d3[lis[0]])
        d3.pop(lis[0])
    elif lis[0] in d2:
        returnDic[lis[0]] = d2[lis[0]]

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.

Python 3 – Key Error = 8

Leave a Reply

Your email address will not be published.