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

Assigning a value to a list variable fails

bajajv
100+
P: 152
Hi,
The below code works:
Expand|Select|Wrap|Line Numbers
  1. newList = ['aa', 'bb', 'cc', 'dd']
  2. newList[2] = 'zz'
  3. print newList
  4. ['aa', 'bb', 'zz', 'dd']
  5.  
But the following code is giving error -
Expand|Select|Wrap|Line Numbers
  1. myDBRow = [''] * len(columnList)
  2. myDBRow[DB.RegionCode] = 'ABC'
  3. TypeError: 'str' object does not support item assignment
  4.  
Can you please suggest something here.

Thanks.
Jan 28 '14 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
The error indicates that myDBRow is a string. Your code appears correct as in this example:
Expand|Select|Wrap|Line Numbers
  1. >>> myDBRow = [''] * 6
  2. >>> myDBRow
  3. ['', '', '', '', '', '']
  4. >>> myDBRow[3]="ABC"
  5. >>> myDBRow
  6. ['', '', '', 'ABC', '', '']
Your error can be reproduced with this example:
Expand|Select|Wrap|Line Numbers
  1. >>> myDBRow = "*" * 6
  2. >>> myDBRow[3]="ABC"
  3. Traceback (most recent call last):
  4.   File "<interactive input>", line 1, in <module>
  5. TypeError: 'str' object does not support item assignment
  6. >>> 
Something is going on that is not apparent in your code.
Jan 28 '14 #2

Expert 100+
P: 620
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.
Jan 28 '14 #3

bajajv
100+
P: 152
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.
Jan 29 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.