Simple question from Python newb... What am I doing wrong? For x, y, z in aTuple:

Amy G amy-g-art at cox.net
Mon Jan 19 17:12:23 EST 2004


Thanks for the help.  Got it working like a dream now.


"Dave Kuhlman" <dkuhlman at rexx.com> wrote in message
news:buhi49$i49rc$1 at ID-139865.news.uni-berlin.de...
> Amy G wrote:
>
> > I have a whole bunch of tuples that look something like this,
> >
> > aTuple = ('1074545869.6580.msg', 't_bryan_pw at gmcc.ab.ca', 'Your
> > one stop prescriptions')
> >
> > now that I have this I try
> >
> > 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. If I do...
>
> The "for" statement processes each item in a sequence object.  The
> first object in your sequence object (aTuple) is a string.  The
> string has more that three characters.  So, that string cannot be
> unpacked into your three variables x, y, and z.
>
> In contrast, consider the following:
>
>     >>> value = (('aa', 'bb', 'cc'), ('dd', 'ee', 'ff'))
>     >>> for x, y, z in value:
>         print 'x:', x
>         print 'y:', y
>         print 'z:', z
>
>     x: aa
>     y: bb
>     z: cc
>     x: dd
>     y: ee
>     z: ff
>     >>>
>
> In this example, the first item in the tuple is itself a tuple
> with three items.  Therefore, it can be unpacked into the
> variables x, y, and z.
>
> Dave
>
> -- 
> http://www.rexx.com/~dkuhlman
> dkuhlman at rexx.com





More information about the Python-list mailing list