Error message <exceptions.TypeError unpack non-sequence>

Discussion in 'Python' started by ahsan Imam, Feb 5, 2004.

  1. ahsan Imam

    ahsan Imam Guest

    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
     
    ahsan Imam, Feb 5, 2004
    #1
    1. Advertising


  2. > 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
     
    Josiah Carlson, Feb 6, 2004
    #2
    1. Advertising

  3. ahsan Imam

    ahsan Imam Guest

    Josiah Carlson <> wrote in message news:<bvv2dr$7oa$>...
    > > 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
     
    ahsan Imam, Feb 6, 2004
    #3
  4. ahsan Imam

    Terry Reedy Guest

    "ahsan Imam" <> wrote in message
    news:...
    > Josiah Carlson <> wrote in message

    news:<bvv2dr$7oa$>...
    > > > 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
     
    Terry Reedy, Feb 7, 2004
    #4
  5. > 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
     
    Josiah Carlson, Feb 7, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Python
    Replies:
    1
    Views:
    2,463
    Tyler Eaves
    Nov 3, 2003
  2. Tim Ronning
    Replies:
    0
    Views:
    420
    Tim Ronning
    Nov 4, 2003
  3. stef mientki
    Replies:
    13
    Views:
    666
    stef mientki
    Oct 20, 2007
  4. ram
    Replies:
    2
    Views:
    431
    Duncan Booth
    Dec 18, 2007
  5. Jaydeep Patil
    Replies:
    4
    Views:
    132
Loading...

Share This Page