Error message <exceptions.TypeError unpack non-sequence>

Josiah Carlson jcarlson at nospam.uci.edu
Thu Feb 5 22:39:28 EST 2004


> 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



More information about the Python-list mailing list