Simple question from Python newb... What am I doing wrong? For x, y, z in aTuple:
Mike C. Fletcher
mcfletch at rogers.com
Mon Jan 19 16:31:28 EST 2004
Amy G wrote:
>I have a whole bunch of tuples that look something like this,
>
>
...
>for x, y, z in aTuple:
> do something with x
> do something with y
> do something with z
>
>But I am getting the error that there are too many values to unpack.
>
>
You have one of two problems, most likely:
* if you really are doing for x,y,z in aTuple (not a list of
aTuples), you're trying to unpack *each* item in aTuple (where
each such item is a string, all of which happen to be much longer
than three items) into three items. Python right complains that
the strings aren't all three characters long.
* if you really are doing for x,y,z in aTupleList (a list of
aTuples), you've got one non-standard tuple in the list, and can
find it pretty easily by doing [ x for x in aTupleList if len(x)
!= 3 ] and then figure out how it got there.
My non-existent money is on the first case.
...
>It prints the items from the tuple each on its own line. How can I unpack
>these three string values into three different string variables like my
>first for loop?
>
>
x,y,z = aTuple
Enjoy yourself,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list