Simple question from Python newb... What am I doing wrong? For x, y, z in aTuple:
Dave Kuhlman
dkuhlman at rexx.com
Mon Jan 19 16:28:44 EST 2004
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