Error message <exceptions.TypeError unpack non-sequence>

A

ahsan Imam

Hello All,

I am trying to move an application from python 1.5.2 to 2.3. The code
works fine in 1.5.2 but gives the exception (exceptions.TypeError
unpack non-sequence) in python 2.3. I did not write this code so I am
not sure what is happening here.

Here is the code snippet:

for (item, agent) in self.lItems:
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))

Note:
self.lItems contains two elements.

Questions:
1) What is the for statement doing?
2) Is this called tuple unpacking or list unpacking?
3) Is there newer syntax?
4) Why does he use the "for" loop like that?

Any help is appreciated.

Thanks
Ahsan
 
J

Josiah Carlson

I am trying to move an application from python 1.5.2 to 2.3. The code
works fine in 1.5.2 but gives the exception (exceptions.TypeError
unpack non-sequence) in python 2.3. I did not write this code so I am
not sure what is happening here.

Here is the code snippet:

for (item, agent) in self.lItems:
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))

Note:
self.lItems contains two elements.

Always exactly 2 items?
Questions:
1) What is the for statement doing?

Attempting to assign the names item and agent a pair of values in
self.lItems
2) Is this called tuple unpacking or list unpacking?

list unpacking:
[item, agent] = [1,2]

tuple unpacking:
item, agent = 1,2
(item, agent) = 1,2
item, agent = (1,2)
(item, agent) = (1,2)

I would be willing to bet that the list below is cast into a tuple:
(item, agent) = [1,2]
3) Is there newer syntax?

I wouldn't so much call it newer as more intuitive.
.... print i, j
....
1 2
3 4
4) Why does he use the "for" loop like that?

Because he doesn't realize he could do the below.
item, agent = self.lItems
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))



- Josiah
 
A

ahsan Imam

Josiah Carlson said:
Always exactly 2 items?
What if there are more than 2 items? How can I do something where
element 0 and 1 are assigned to (item, agent) and so on? If this is a
silly question please let me know what I can read.

Thanks
Ahsan
 
T

Terry Reedy

[/QUOTE]

I do not know of any change in Python that would make code like the below
invalid. Are you possibly running the program with different input data?
I suggest you insert 'print self.lItems' before the loop to see it *that*
changed (somewhere else in the program).

self.lItems must contain zero or more (item,agent) *pairs*
What if there are more than 2 items? How can I do something where
element 0 and 1 are assigned to (item, agent) and so on?

Yes and no. You would have to group elements 0 and 1 into a pair, elements
2 and 3 into another (the second), and so on. There is probably something
in itertools that will do this. Otherwise, writing your own generator to
do so should be easy enough. Then write 'for (i,a) in grouper(s.l):' where
grouper is the pair generator.

Terry J. Reedy
 
J

Josiah Carlson

What if there are more than 2 items? How can I do something where
element 0 and 1 are assigned to (item, agent) and so on? If this is a
silly question please let me know what I can read.

If there ever was more than two items, that is, if it was a flat
sequence like this: [1,2,3,4,5,6], then the original for loop couldn't
have worked.

- Josiah
 

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

No members online now.

Forum statistics

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

Latest Threads

Top