Search code examples
pythondictionarykey-valuevalueerror

too many values to unpack on dictionary key value iteration


Getting an error too many values to unpack. Can anyone help me to solve it?

friendship = {'nino': ["tamari", "nika", "lela", "dato"],
              'dato': ["tamari", "nino"],
              'tamari': ["nino", "dato", "lela"],
              'nika': ["nino"],
              'lela': ["nino", "tamari"]
}
def f(**friendship):
    sia={}
    for i in friendship.values():
        m = min(i)
        for k,v in friendship.items():
            sia.update({k:(len(v))})
        low = min(sia.values())
        res = [x for x,y in sia if sia.items() if y == low]

    print(str(res) + " has " + str(low) + " friends")   ------ getting an error on this line.
print (f(**friendship))

Solution

  • The problem is the unpacking at x,y in sia to have key/value use items() and remove the useless if, like this

    [x for x, y in sia.items() if y == low]
    

    Also you can remove the ** operator at both places because it's useless it you directly pass a dict, and it causes to flat the data, then pack it again in the method