Assigning a list to a key of a dict

Gary Herron gherron at islandtraining.com
Thu May 14 11:59:10 EDT 2009


Wells wrote:
> Why can't I do this?
>
> teams = { "SEA": "Seattle Mariners" }
> for team, name in teams.items():
>         teams[team]["roster"] = ["player1", "player2"]
>   

Because,
  team will be "SEA",
so 
  teams[team] will be  "Seattle Mariners"
and
  "Seattle Mariners"["roster"] makes no sense.



Gary Herron


> I get an error:
>
> Traceback (most recent call last):
>   File "./gamelogs.py", line 53, in <module>
>     teams[team]["roster"] = ["player1", "player2"]
> TypeError: 'str' object does not support item assignment
>
> You can see that I am trying to make a key called "roster" for each
> team item in the dictionary, which is a list of players.
>   




More information about the Python-list mailing list