Assigning a list to a key of a dict

W

Wells

Why can't I do this?

teams = { "SEA": "Seattle Mariners" }
for team, name in teams.items():
teams[team]["roster"] = ["player1", "player2"]

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

Diez B. Roggisch

Wells said:
Why can't I do this?

teams = { "SEA": "Seattle Mariners" }
for team, name in teams.items():
teams[team]["roster"] = ["player1", "player2"]

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.

This is not a list as key, it's a nested dictionary. There are several
approaches to this:

- make your keys tuples, like this:

- use setedfault:
teams.setdefault(team, {})["roster"] = ['player1']

- defaultdict
from collections import *
defaultdict
teams = defaultdict(dict)
teams["team"]["roster"] = ["player1"]
teams

Diez
 
C

Chris Rebert

Why can't I do this?

teams = { "SEA": "Seattle Mariners" }
for team, name in teams.items():
       teams[team]["roster"] = ["player1", "player2"]
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

teams - a dict of str to str
teams[team] - a string (specifically, "Seattle Mariners" in this case)
teams[team]["roster"] - um, you can't subscript a string by another
string; you can subscript it only by integer indices
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.

I'd say you need to restructure your data structure by introducing
another level of dictionaries:

teams = { "SEA": {"full_name":"Seattle Mariners"} }
for team, name in teams.items():
teams[team]["roster"] = ["player1", "player2"]
print teams
#output: {'SEA': {'full_name': 'Seattle Mariners', 'roster':
['player1', 'player2']}}

Of course, it would probably be nicer if you did this using objects
(e.g. define a Team class and have `teams` be a dict of str to Team.

Cheers,
Chris
 
G

Gary Herron

Wells said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top