Python Forum
'str' object does not support item assignment
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'str' object does not support item assignment
#1
So currently i am experiencing an issue where i am trying to assign a value in a dictionary to an index value of a string


The code goes as follows


    searchValues = ['name', 'fame', 'game']
    values = {}

    versions = 5
    for v in range(1, versions+1):
         for value in searchValues:
             receivedValue = customFindFunction("{0}{1}".format(value, v), source)
             if (receivedValue is None):   #the find function returns None if it cant find the value specified
                 receivedValue = customFindfunction(value, source)
                 values[value] = receivedValue
                 continue
             valueKey = value + str(v)
             values[valueKey] = receivedValue  # ERROR OCCURS HERE, the receivedValue is "5"
The custom find function just searches a source string for a key (the value) and returns the actual value of the key (eg: string has Name : John, located in it, the function returns John, or None if it cant find the key)

Whenever the value is returned as None the first time, and it finds a value based on just a searchvalue, it enters into the values dict just fine

Whenever it finds the value on the first try and tries to enter a value into the values dict as values[value+v] it returns an error of "TypeError: 'str' object does not support item assignment"

This isnt the actual code, as the real code has quite a few other things going on during this that do not effect the code that is failing, so i left it out. The code functions fully and returns appropriate values when it finds a value based on its name eg: it finds "name" or "fame", but when it finds a value based on the value name plus a version number eg: "name1" or "fame1" it errors out
Reply
#2
Please, check your actual code and ensure that you don't overwrite your dict values with some string (i.e. probably mistake where you typed values instead of value)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove an item from a list contained in another item in python CompleteNewb 19 5,556 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  'int' object does not support item assignment shane1236 5 7,353 Aug-13-2019, 01:53 PM
Last Post: buran
  Error: int object does not support item assignment ankita_nthu 2 13,881 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  How do I use this? TypeError: 'NoneType' object does not support item assignment ejected 9 22,175 Mar-26-2019, 05:06 AM
Last Post: ejected
  ValueError: This COM object does not support events. meenakshi11 0 3,861 Mar-08-2018, 09:42 AM
Last Post: meenakshi11

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020