problems when unpacking tuple ...

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Sat Apr 22 13:21:10 EDT 2006


harold:
>A similar error happens in an interpreter session, when typing
>>>> for line in ["1 2 3 4"] :
>...    for a,b,c,d in line.split() :
>...        pass
>...
>Traceback (most recent call last):
>  File "<stdin>", line 2, in ?
>ValueError: need more than 1 value tyo unpack
>
>maybe this might help to track down the error.

Suppose the code was:

    for x in line.split():

line.split() yields one list with 4 items. The loop will be performed 4
times, assigning one value of the list to x with every iteration. In your
code, x is a tuple with 4 elements: a,b,c,d. So with every iteration one
value is assigned to that tuple. Since the value is not a sequence of 4
items, this fails.

There's two sensible things you can do:

for line in ["1 2 3 4"]:
    a,b,c,d = line.split()

for line in ["1 2 3 4"]:
    for a in line.split():

-- 
René Pijlman



More information about the Python-list mailing list