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

I am trying to create / update a dictionary. I have aliases as keys in temp_dict and search for identifiers to add.

Excerpt from my code. I think you just need 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 mistake:

 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 several SO records regarding the same error. But none of them are related to such dictionary manipulations.

+8
python dictionary
source share
1 answer

Most likely you put the unicode line 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 

run it with an empty dict and everything will work:

 >>> temp_dict = {} >>> temp_dict[x] = y >>> temp_dict {u'45sss': 553472} 
+8
source share

All Articles