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

A

Amy G

I have a whole bunch of tuples that look something like this,

aTuple = ('1074545869.6580.msg', '(e-mail address removed)', '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...

for x in aTuple:
print x

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?

Thanks in advance for answering what is probably a simple question.
 
D

Dave Kuhlman

Amy said:
I have a whole bunch of tuples that look something like this,

aTuple = ('1074545869.6580.msg', '(e-mail address removed)', '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:
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
 
M

Mike C. Fletcher

Amy said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top