why does this code not work and gives back (TypeError: 'str' object does not support item assignment) :

sharesOwned = {'name': 'Apple.inc', 'symbol': 'AAPL', 'price':110, 'name': 'Google', 'symbol': 'GOOGL', 'price':330, }

for share in sharesOwned:
    shareNow = {'name': 'WrongApple.inc', 'symbol': 'WrongAAPL', 'Wrongprice':10}
    share['priz"'] = shareNow['name']

print (share['priz'])

How do we correctly assign from one dict to another?

1 Answer 1


'share' a string variable that holds the values of an item in 'sharesOwned' so it is not a dictionary. If you took the square brackets away:

share = shareNow['name']

This would allow the assignment. To assign from one dictionary to another you have to initialise it as a dictionary. Once you do this then the syntax your using is correct, just remember to correct the extra " typo.

  • Thanks a bunch!
    – nvs0000
    May 20, 2017 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .