Biggen
Biggen

Reputation: 295

Dictionary format with dicts in lists in dict

Is something like this even possible??

{'SEC': [{'t_name': 'UGA', 't_clubhouse': 'www.uga.com'}],
        [{'t_name': 'BAMA', 't_clubhouse': 'www.bama.com'}],
        [{'t_name': 'VANDY', 't_clubhouse': 'www.vandy.com'}]}

I know you have to have unique keys but I'm struggling to understand how to create a dictionary like above since I need everything grouped into a conference. Here are some generic lists I'm trying to create the dict from:

conference = 'SEC'
team_list = ['UGA', 'BAMA', 'VANDY']
team_clubhouse = ['uga.com', 'bama.com', 'vandy.com']

I'm wanting to throw all of this into a db but for now, I'd like to create the dict first and then be able to pipe that into Peewee for insertion.

Upvotes: 0

Views: 48

Answers (2)

Eric Ed Lohmar
Eric Ed Lohmar

Reputation: 1922

I'm not sure you need a list here. Just deep nest some dicts.

NCAA = {'SEC': {'team1': {'nickname': 'UGA', 'clubhouse': 'uga.com'}},
               {'team2': {'nickname': 'BAMA', 'clubhouse': 'bama.com'}},
               {'team3': {'nickname': 'VANDY', 'clubhouse': 'vandy.com'}},
         'Big_10': {'Purdue': {'nickname': 'Boilers', 'clubhouse': 'boiler_up.com'}}}

I think that this question is similar and can provide a little more info

Upvotes: 1

falsetru
falsetru

Reputation: 368894

Using list comprehension:

>>> team_list = ['UGA', 'BAMA', 'VANDY']
>>> team_clubhouse = ['uga.com', 'bama.com', 'vandy.com']
>>> [{'t_name': team, 't_clubhouse': clubhouse}
     for team, clubhouse in zip(team_list, team_clubhouse)]
[{'t_name': 'UGA', 't_clubhouse': 'uga.com'},
 {'t_name': 'BAMA', 't_clubhouse': 'bama.com'},
 {'t_name': 'VANDY', 't_clubhouse': 'vandy.com'}]

Above creates a list of dictionaries.

Map a key conference to the list to get the dictionary you want (You can map a key to a value, not multiple values):

>>> conference = 'SEC'
>>> {conference: [{'t_name': team, 't_clubhouse': clubhouse}
     for team, clubhouse in zip(team_list, team_clubhouse)]}
{'SEC': [
    {'t_name': 'UGA', 't_clubhouse': 'uga.com'},
    {'t_name': 'BAMA', 't_clubhouse': 'bama.com'},
    {'t_name': 'VANDY', 't_clubhouse': 'vandy.com'}
]}

Upvotes: 3

Related Questions