By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Error message <exceptions.TypeError unpack non-sequence>

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
for i,j in [(1,2), (3,4)]: .... 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
Jul 18 '05 #2

P: n/a
Josiah Carlson <jc******@nospam.uci.edu> wrote in message news:<bv**********@news.service.uci.edu>...
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?

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
Jul 18 '05 #3

P: n/a

"ahsan Imam" <ah********@newtimes.com> wrote in message
news:78**************************@posting.google.c om...
Josiah Carlson <jc******@nospam.uci.edu> wrote in message news:<bv**********@news.service.uci.edu>...
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.
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).

Here is the code snippet:

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


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


Jul 18 '05 #4

P: n/a
> 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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.