Checking if a list is already in the dictionary in python -
i trying check if list s in dictionary in python. code supposed generate 2 random numbers(r1 , r2) , append them list in dictionary if same 2 numbers aren't in there. here code:
main_dict = {0:[], 1:[], 2:[], 3:[], 4:[]} x in range(0,5): r1 = randint(1,5) r2 = randint(1,5) temp = [r1,r2] if temp not in main_dict: main_dict[x].append(r1) main_dict[x].append(r2)
so main_dict should this: {0:[2,3],1:[4,1],2:[3,3],3:[3,2],4:[5,1]}, , code above should take care no combination repeated.
the error "typeerror:unhashable type: 'list'", , guess because can't put list next if, have no idea else put, have tried came mind.
thanks in advance :)
the problem getting because looking in dictionary if list exists. that, comparing list keys of dictionary. want is:
if temp not in manin_dict.values():
Comments
Post a Comment