python ValueError: too many values to unpack in tuple

10,005

Solution 1

What you want is

text, ans, qid = hist
print(text)

instead of

for text, ans, qid in hist:

Think about what hist represents - it is a single tuple (because you've looked it up with key)

That means that

for text, ans, qid in hist:

is trying to iterate through each member of the tuple and break them into those three components. So, first, it tries to act on hist[0] i.e. "This type of year...." and tries to break it into text, ans and qid. Python recognises that the string could be broken up (into characters), but can't work out how to break it into those three components as there are far more characters. So it throws the Error 'Too many values to unpack'

Solution 2

What your loop is trying to do is iterate through the first three items of hist and interpret EACH of them individually as a three-element tuple. I'd guess what you are trying to do is this:

for key in dic:
    hist = dic[key]
    (text, ans, qid) = hist[0:3] # Might not need this slice notation if you are sure of the number of elements
    print(text)

Solution 3

change this:

for text, ans, qid in hist[0:2]:

to this:

for text, ans, qid in hist[0:3]:

hist[x:y] is all the elements from hist with x <= ids < y

EDIT:

As pointed out by @J Richard Snape and @rchang you can't use this:

for text, ans, qid in hist[0:3]:

but you can use this instead (worked for me):

for text, ans, qid in [hist[0:3]]:
Share:
10,005
KameeCoding
Author by

KameeCoding

Updated on July 13, 2022

Comments

  • KameeCoding
    KameeCoding almost 2 years

    So I am extracting data from a JSON file.

    I am trying to pack my data in a way so a preprocessing script can use it.

    preprocessing script code:

    for key in split:
        hist = split[key]
        for text, ans, qid in hist:
    

    Now I have an extracted dataset into a Dictionary like this:

    dic{}
    result //is the result of removing some formatting elements and stuff from the Question, so is a question string
    answer //is the answer for the Q
    i // is the counter for Q & A pairs
    

    So I have

    this = (result,answer,i)
    dic[this]=this
    

    And when I try to replicate the original code I get the Too many values to unpack error

    for key in dic:
        print(key)
        hist = dic[key]
        print(hist[0])
        print(hist[1])
        print(hist[2])
        for text, ans, qid in hist[0:2]:  // EDIT: changing this to hist[0:3] or hist has no effect
            print(text)
    

    OUTPUT:

    (u'This type of year happens once every four', u'leap', 1175)
    This type of year happens once every four
    leap
    1175
    Traceback (most recent call last):
      File "pickler.py", line 34, in <module>
        for text, ans, qid in hist[0:2]:
    ValueError: too many values to unpack
    

    As you can see I even tried limiting the right side of the assignment but that didn't help either

    and as you can see the output matches as it should for each item

    hist[0]=This type of year happens once every four
    hist[1]=leap
    hist[2]=1175
    

    And len(hist) returns 3 also.

    Why the hell is this happening? Having hist,hist[:3],hist[0:3] has the same result, Too many values to unpack error.

  • KameeCoding
    KameeCoding over 9 years
    same result @JRichardSnape
  • KameeCoding
    KameeCoding over 9 years
    same result @JasonPap
  • rchang
    rchang over 9 years
    @Kameegaming I tried to replicate as much of your situation as I could here: repl.it/8aC (click "run session"). Did I overlook something?
  • J Richard Snape
    J Richard Snape over 9 years
    Sorry - I fell into a very silly trap - see revised answer. Note also - @rchang is right - he's not advising you to iterate over hist[0:3]
  • KameeCoding
    KameeCoding over 9 years
    seems correct, this one actually works, even if I leave [0:3] out, but doesn't work with for, which is weird, huh
  • J Richard Snape
    J Richard Snape over 9 years
    Good answer. See my slightly longer one for an explanation why it doesn't work with for
  • rchang
    rchang over 9 years
    Upvoting for the clear explanation of the source of the unpacking error. :)