Search code examples
pythonstringdictionaryargumentsiterable-unpacking

More than one value to unpack


I run this code:

def score(string, dic):
    for string in dic:
        word,score,std = string.lower().split()
        dic[word]=float(score),float(std)
        v = sum(dic[word] for word in string)
        return float(v)/len(string)

And get this error:

word,score,std = string.split()
ValueError: need more than 1 value to unpack

Solution

  • It's because string.lower().split() is returning a list with only one item. You cannot assign this to word,score,std unless this list has exactly 3 members; i.e. string contains exactly 2 spaces.


    a, b, c = "a b c".split()  # works, 3-item list
    a, b, c = "a b".split()  # doesn't work, 2-item list
    a, b, c = "a b c d".split()  # doesn't work, 4-item list