Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

like image 357
Aidis Avatar asked Jan 28 '14 20:01

Aidis


1 Answers

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}
like image 166
ndpu Avatar answered Oct 23 '22 19:10

ndpu