Home > Software design >  How to iterate through a list and add them to the values inside nested dictionary?
How to iterate through a list and add them to the values inside nested dictionary?

Time:10-30

  • GXX = game_id
player_statistics = 
{'P001': {'G02': ['1'], 'G05': ['0'], 'G07': ['0'], 'G08': ['0'], 'G10': ['1']},
 'P067': {'G02': ['1'], 'G05': ['1'], 'G07': ['0'], 'G08': ['0'], 'G10': ['0']},
 'P218': {'G02': ['0'], 'G05': ['1'], 'G07': ['1'], 'G08': ['0'], 'G10': ['0']},
 'P101': {'G02': ['0'], 'G05': ['0'], 'G07': ['1'], 'G08': ['1'], 'G10': ['0']},
 'P456': {'G02': ['1'], 'G05': ['1'], 'G07': ['0'], 'G08': ['1'], 'G10': ['0']}}

game_info = [
['G05', 'Hopscotch', 'N', '2'],
 ['G07', 'Dominoes', 'S', '5'],
 ['G08', 'Chess', 'N', '3'],
 ['G10', 'Monopoly', 'N', '3'],
 ['G02', 'MineCraft', 'S', '5']
]

I want to add index[2] and index[3] from each list in the game_info to the value inside value (list) in the player_statistics.

My code:

for values in player_statistics.values():
    for k, v in values.items():
        for data in game_info:
            if k == data[0]:
                player_statistics[k].insert(1, data[2])
                player_statistics[k].insert(2, data[3])

My code Output:

Traceback (most recent call last):
  File "D:\projects\FinalCodingChallenge\test.py", line 25, in <module>
    player_statistics[k].insert(1, data[2])
KeyError: 'G02'

Expected Output:

player_statistics = 
{'P001': {'G02': ['1', 'S', '5'], 'G05': ['0', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['1', 'N', '3']},
 'P067': {'G02': ['1', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['0', 'N', '3']},
 'P218': {'G02': ['0', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['1', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['0', 'N', '3']},
 'P101': {'G02': ['0', 'S', '5'], 'G05': ['0', 'N', '2'], 'G07': ['1', 'S', '5'], 'G08': ['1', 'N', '3'], 'G10': ['0', 'N', '3']},
 'P456': {'G02': ['1', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['1', 'N', '3'], 'G10': ['0', 'N', '3']}}

I would appreciate if you could explain what I did wrong on my code, and show me the appropriate way to do it. Thanks in advance.

CodePudding user response:

Try:

import pprint

player_statistics = {'P001': {'G02': ['1'], 'G05': ['0'], 'G07': ['0'], 'G08': ['0'], 'G10': ['1']},
                     'P067': {'G02': ['1'], 'G05': ['1'], 'G07': ['0'], 'G08': ['0'], 'G10': ['0']},
                     'P218': {'G02': ['0'], 'G05': ['1'], 'G07': ['1'], 'G08': ['0'], 'G10': ['0']},
                     'P101': {'G02': ['0'], 'G05': ['0'], 'G07': ['1'], 'G08': ['1'], 'G10': ['0']},
                     'P456': {'G02': ['1'], 'G05': ['1'], 'G07': ['0'], 'G08': ['1'], 'G10': ['0']}}

game_info = [
    ['G05', 'Hopscotch', 'N', '2'],
    ['G07', 'Dominoes', 'S', '5'],
    ['G08', 'Chess', 'N', '3'],
    ['G10', 'Monopoly', 'N', '3'],
    ['G02', 'MineCraft', 'S', '5']
]


lookup = { key : data  for key, _, *data in game_info }

for key, player in player_statistics.items():
    for key, value in player.items():
        if key in lookup:
            value.extend(lookup[key])


pprint.pprint(player_statistics)

Output

{'P001': {'G02': ['1', 'S', '5'],
          'G05': ['0', 'N', '2'],
          'G07': ['0', 'S', '5'],
          'G08': ['0', 'N', '3'],
          'G10': ['1', 'N', '3']},
 'P067': {'G02': ['1', 'S', '5'],
          'G05': ['1', 'N', '2'],
          'G07': ['0', 'S', '5'],
          'G08': ['0', 'N', '3'],
          'G10': ['0', 'N', '3']},
 'P101': {'G02': ['0', 'S', '5'],
          'G05': ['0', 'N', '2'],
          'G07': ['1', 'S', '5'],
          'G08': ['1', 'N', '3'],
          'G10': ['0', 'N', '3']},
 'P218': {'G02': ['0', 'S', '5'],
          'G05': ['1', 'N', '2'],
          'G07': ['1', 'S', '5'],
          'G08': ['0', 'N', '3'],
          'G10': ['0', 'N', '3']},
 'P456': {'G02': ['1', 'S', '5'],
          'G05': ['1', 'N', '2'],
          'G07': ['0', 'S', '5'],
          'G08': ['1', 'N', '3'],
          'G10': ['0', 'N', '3']}}

CodePudding user response:

Looks like you missed the keys from the first for loop. It should work if you add it:

for keys, values in player_statistics.items():
    for k, v in values.items():
        for data in game_info:
            if k == data[0]:
                player_statistics[keys][k].insert(1, data[2])
                player_statistics[keys][k].insert(2, data[3])

print(player_statistics)

CodePudding user response:

Based on my understanding, with

player_statistics[k].insert()

you are trying to find game "G02" in the players list ("P001", "P061", ...)

for values in player_statistics.values():
    for k, v in values.items():
        for data in game_info:
            if k == data[0]:
                player_statistics[k].insert(1, data[2])
                player_statistics[k].insert(2, data[3])
  • Related