How to select a dict key that is inside a list of dicts holding list and more dicts

Question:

I have a list called:

all_players = []

A method called get_teams() which takes an URL and returns a list that holds dicts of a team name and URL of the team, and example of the output will be

teams = get_teams(url)

will return

[{'name': 'Dallas', 'url': 'https://en.wikipedia.org/wiki/2021%E2%80%9322_Dallas_Mavericks_season'}, {'name': 'Phoenix', 'url': 'https://en.wikipedia.org/wiki/2021%E2%80%9322_Phoenix_Suns_season'},...

A method called get_player(url), that takes the URL of the team and returns a list holding dicts of names and URL of the player, a return will look like this:

[{'name': 'Jaylen Brown', 'url': 'https://en.wikipedia.org/wiki/Jaylen_Brown'}, {'name': 'Malik Fitts', 'url': 'https://en.wikipedia.org/wiki/Malik_Fitts'}, {'name': 'Sam Hauser', 'url': 'https://en.wikipedia.org/wiki/Sam_Hauser'},...

So, what I have done so far is this:

for x in teams:
    all_players.append({x['name']: get_players(x['url'])})

printing out with this for loop:

for c in all_players:
     print(c)
     print()

will return this

{'Dallas': [{'name': 'Dāvis Bertāns', 'url': 'https://en.wikipedia.org/wiki/D%C4%81vis_Bert%C4%81ns'}, {'name': 'Sterling Brown (basketball)', 'url': 'https://en.wikipedia.org/wiki/Sterling_Brown_(basketball)'},...

in the format

[{"team name": [{ "name": "player name", "url": "https://player_url"},...

I will try to explain what I want to do as clear as I can 🙂

What I want to do is, to take the url that is inside all_players ( this on: "url": "https://player_url") which means, go inside the list, inside the dict , get the value of the team name, this value is a list, so I want to go inside the list and take the url value. With this url value I want to use a method called get_player_stats(url, teamname) which will add 3 more keys to the dict which is inside the team name value list.

I have absolutely no idea how to take the url and then add new values to that dict which are returned by the method.

Any hint or idea would be highly appreciated.

just an example on how the final dict inside the all_players would look like:

{     "team name": [{ "name": "player name", "url": "https://player_url", "newkey1":1, "newkey2: 2, "newkey3":3 },...]}

What I have tried:

for x in all_players:
    for team, players in x.items():
        for n in players:   
            print(n['url]')

Using 3 loops I can get into the url, and i can use get_player_stats(), but I’m not sure how to append this to all_players the way I described over.

Asked By: ili

||

Answers:

You were close to the answer. So in this step of your program your all_players list should look something like this:

all_players = {
    "Dallas": [
        {
            "name": "Dāvis Bertāns",
            "url": "https://en.wikipedia.org/wiki/D%C4%81vis_Bert%C4%81ns",
        },
        {
            "name": "Sterling Brown (basketball)",
            "url": "https://en.wikipedia.org/wiki/Sterling_Brown_(basketball)",
        },
    ],
    "Team2": [
        {
            "name": "sample1",
            "url": "https://en.wikipedia.org/wiki/sample1",
        },
        {
            "name": "sample2",
            "url": "https://en.wikipedia.org/wiki/sample2",
        },
    ],
}

You need to first get the player’s url, then call get_player_stats function. Here is the code:

from pprint import pprint

def get_player_stats(url, team_name):
    # get information based on `url`.
    team_name["newkey1"] = 1
    team_name["newkey2"] = 2


for team, players in all_players.items():
    for player in players:
        player_url = player["url"]
        get_player_stats(player_url, player)

pprint(all_players, sort_dicts=False)

Look at the loop, when you get the URL, you also have access to the player dictionary which is player. So pass it to the get_player_stats and it can mutate it! team_name is your player dictionary.

output:

{'Dallas': [{'name': 'Dāvis Bertāns',
             'url': 'https://en.wikipedia.org/wiki/D%C4%81vis_Bert%C4%81ns',
             'newkey1': 1,
             'newkey2': 2},
            {'name': 'Sterling Brown (basketball)',
             'url': 'https://en.wikipedia.org/wiki/Sterling_Brown_(basketball)',
             'newkey1': 1,
             'newkey2': 2}],
 'Team2': [{'name': 'sample1',
            'url': 'https://en.wikipedia.org/wiki/sample1',
            'newkey1': 1,
            'newkey2': 2},
           {'name': 'sample2',
            'url': 'https://en.wikipedia.org/wiki/sample2',
            'newkey1': 1,
            'newkey2': 2}]}
Answered By: S.B
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.