python: ValueError: not enough values to unpack (expected 2, got 0)

simplesystems picture simplesystems · Oct 29, 2015 · Viewed 39.4k times · Source

What does this error mean?

labels, freq = zip(*terms_hash)
ValueError: not enough values to unpack (expected 2, got 0) 

When I just print the terms out there is no error.

code:

fname = 'stream.json'
with open(fname, 'r') as f:
    print('alle Hashtags')
    count_all = Counter()
    for line in f:
        tweet = json.loads(line)
# Count hashtags only

        terms_hash = [term for term in preprocess(tweet['text']) 
              if term.startswith('#')]

        # Update the counter
        count_all.update(terms_hash)

        terms_hash = count_all.most_common(5)

        labels, freq = zip(*terms_hash)
        data = {'data': freq, 'x': labels}
        bar = vincent.Bar(data, iter_idx='x')
        bar.to_json('term_freq.json')    


    # Print the first 5 most frequent words
    print(count_all.most_common(5))

Answer

John Gordon picture John Gordon · Oct 29, 2015

It means that python expected there to be two return values from zip(), but there were none.