I'm trying to delete every dictionary that has a point value of 0 but when I run this code the object still remains. Is there a special case here why it won't delete?
import json
import numpy as np
import pandas as pd
from itertools import groupby
# using json open the player objects file and set it equal to data
with open('Combined_Players_DK.json') as json_file:
player_data = json.load(json_file)
for player in player_data:
for points in player['Tournaments']:
player['Average'] = round(sum(float(tourny['Points']) for tourny in player['Tournaments']) / len(player['Tournaments']),2)
for players in player_data:
for value in players['Tournaments']:
if value['Points'] == 0:
del value
with open('PGA_Player_Objects_With_Average.json', 'w') as my_file:
json.dump(player_data, my_file)
Here is the JSON
[
{
"Name": "Dustin Johnson",
"Tournaments": [
{
"Date": "2020-06-25",
"Points": 133.5
},
{
"Date": "2020-06-18",
"Points": 101
},
{
"Date": "2020-06-11",
"Points": 25
},
{
"Date": "2020-02-20",
"Points": 60
},
{
"Date": "2020-02-13",
"Points": 89.5
},
{
"Date": "2020-02-06",
"Points": 69.5
},
{
"Date": "2020-01-02",
"Points": 91
},
{
"Date": "2019-12-04",
"Points": 0
}
],
"Average": 71.19
}]
I'm not sure why I can't use the delete value. I tried remove as well but then I was left with an empty object.