Assigning a value to a list variable fails

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    Assigning a value to a list variable fails

    Hi,
    The below code works:
    Code:
    newList = ['aa', 'bb', 'cc', 'dd']
    newList[2] = 'zz'
    print newList
    ['aa', 'bb', 'zz', 'dd']
    But the following code is giving error -
    Code:
    myDBRow = [''] * len(columnList)
    myDBRow[DB.RegionCode] = 'ABC'
    TypeError: 'str' object does not support item assignment
    Can you please suggest something here.

    Thanks.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The error indicates that myDBRow is a string. Your code appears correct as in this example:
    Code:
    >>> myDBRow = [''] * 6
    >>> myDBRow
    ['', '', '', '', '', '']
    >>> myDBRow[3]="ABC"
    >>> myDBRow
    ['', '', '', 'ABC', '', '']
    Your error can be reproduced with this example:
    Code:
    >>> myDBRow = "*" * 6
    >>> myDBRow[3]="ABC"
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    TypeError: 'str' object does not support item assignment
    >>>
    Something is going on that is not apparent in your code.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      The first thing to check for is, was "myDBRow" used as a separate variable name somewhere else. Do a search for the name to see if it is used elsewhere.

      Comment

      • bajajv
        New Member
        • Jun 2007
        • 152

        #4
        Hi,
        Yes, you were right.
        My mistake, I accidently made it a string at some place, and that was the reason of this error.
        Sorry for the trouble.
        Appreciate your responses.
        Thanks.

        Comment

        Working...