user2004245
user2004245

Reputation: 399

Pythone ValueError with Tuple being correct length

I'm getting the following error:

Traceback (most recent call last):
  File "C:/Users/user/Documents/Data Munger/new_munger.py", line 49, in <module>
    for a, b in temp_tuple:
ValueError: too many values to unpack (expected 2)

from the following code:

for key in d:
    for temp in d[key]:
        temp_tuple = (temp[0], [temp[i] for i in range(1, len(temp))])
        print(len(temp_tuple))
        e = defaultdict(list)
        for a, b in temp_tuple:
            e.setdefault(a, []).append(b)

The print(len(temp_tuple)) line is spitting out 2 in the console. I can't figure out why this error is being raised.

Thanks for the help.

Upvotes: 0

Views: 176

Answers (2)

Scott Hunter
Scott Hunter

Reputation: 49803

While the temp_tuple may be of length 2, your for wants each ITEM in temp_tuple to be 2 items (i.e. a tuple of length 2 tuples).

Upvotes: 0

mhlester
mhlester

Reputation: 23221

Your for loop is already iterating over temp_tuple. In the first instance, you're trying to unpack temp[0] to a and b. Possibly what you meant to do is:

a, b = temp_tuple
e.setdefault(a, []).append(b)

Upvotes: 2

Related Questions