'TypeError: list indices must be integers or slices, not dict python error

So im trying to get this code to work and im quite new at coding with python, im using pysimplegui too and im getting this error 'TypeError: list indices must be integers or slices, not dict'

with this code

    name_arr = [];

    with open('team.json') as json_file:
        data = json.load(json_file)

    for i in data['team']:
        name_arr.append(i['Team Name'])

    #Sort names alphabetically
    name_arr = sorted(name_arr)

    event_arr = ['Basketball','100m','Climbing']
        
    layout = [[sg.Button('Main menu')],
              [sg.Text('Record result for:'),sg.Combo(name_arr)],
              [sg.Text('Event:'),sg.Combo(event_arr)],
              [sg.Text('Finishing position:'),sg.Combo(list(range(1,21)))],
              [sg.Button('Save result')]]

    window = sg.Window('My Tournament Program - record result',layout,size=(700, 600))

    while True:#Keep the window open until button pressed
        event, values = window.Read()
        if event is None or event == "Done":
            break
        if event == "Main menu":
            print('Loading main menu window...')
            window.Close()
            self.main_menu()
            break
        if event == "Save result":
            print('Saving result... add more code below')
            #Open competitors file
            with open('team.json') as json_file:
               data = json.load(json_file)

            for i in data['team']:
               #Find the selected competitor by their name
               if i['Team Name'] == values[0]:
                   #name found in file
                   #Update to include their result
                   #Score is 21 minus finishing position e.g. 20 pts for 1st
                   data['team'][i]['Score'] = 20 - values[2]

            #Save data competitors file
            try:
                with open('team.json', 'w') as f:
                    json.dump(data, f)
                    print("Score saved succesfully.")

            except IOError as e:
                    print("I/O error({0}): {1}").format(e.errno, e.strerror)
                    print("Error saving team")
    window.Close()

can anyone help me figure it out, im guessing its something to do with this line

data['team'][i]['Score'] = 20 - values[2]

but not sure..



Solution 1:[1]

In this for loop:

            for i in data['team']:
               #Find the selected competitor by their name
               if i['Team Name'] == values[0]:
                   #name found in file
                   #Update to include their result
                   #Score is 21 minus finishing position e.g. 20 pts for 1st
                   data['team'][i]['Score'] = 20 - values[2]

i is an actual item in data['team'] (that is, it's a dict in a list of dicts), not the index of one of those items (which would be an int).

Since i is the dict you want to modify, you should be able to just change the offending line to:

                   i['Score'] = 20 - values[2]

Unrelated, I'd suggest changing this line:

event, values = window.Read()

to something like:

event, (team_name, _, finishing_pos) = window.Read()

so that you don't have to remember the individual indices of values:

            for i in data['team']:
               #Find the selected competitor by their name
               if i['Team Name'] == team_name:
                   #name found in file
                   #Update to include their result
                   #Score is 21 minus finishing position e.g. 20 pts for 1st
                   i['Score'] = 20 - finishing_pos

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1