Aidis
Aidis

Reputation: 1280

"TypeError: 'unicode' object does not support item assignment" in dictionaries

I am trying to build/update a dictionary. I have nicknames as keys in temp_dict and looking for ids to add.

Excerpt form my code. I think it is enough for you to see my mistake.

d1 = {u'status': u'ok', u'count': 1, u'data': [{u'nickname': u'45sss', u'account_id': 553472}]}


   temp_dict = {}
   for key, value in d1.iteritems():
        if "data" == key:
            for dic2 in value:
                  x = dic2['nickname']
                  y = dic2['account_id']
                  temp_dict[x] = y;

My error:

Traceback (most recent call last):
File "untitled.py", line 36, in <module>
get_PlayerIds_Names_WowpApi_TJ_() #Easy going. Some issues with case letters.
File "g:\Desktop\Programming\WOWP API\functions.py", line 44, in get_PlayerIds_Names_WowpApi_TJ_
check_missing_player_ids(basket)
File "g:\Desktop\Programming\WOWP API\functions.py", line 195, in check_missing_player_ids
temp_dict[x] = y;
TypeError: 'unicode' object does not support item assignment

There are multiple SO entries regarding the same error. But no are connected to such dictionary manipulation.

Upvotes: 10

Views: 50768

Answers (1)

ndpu
ndpu

Reputation: 22571

Most likely you have put unicode string in temp_dict somewhere:

>>> temp_dict = u''
>>> dic2 = {u'nickname': u'45sss', u'account_id': 553472}
>>> x = dic2['nickname']
>>> y = dic2['account_id']
>>> temp_dict[x] = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'unicode' object does not support item assignment

init it with empty dict and all will work:

>>> temp_dict = {}
>>> temp_dict[x] = y
>>> temp_dict
{u'45sss': 553472}

Upvotes: 16

Related Questions