1

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.

Austin Johnson
  • 697
  • 11
  • 23
  • Even when I ```print(value['Points'] == 0)``` it returns true – Austin Johnson Jul 02 '20 at 01:46
  • 1
    You want `del players['Tournaments']` for the deletion to act on the dictionary & remove that entry, and not `del value` which only deletes the `value` name from the scope. See [the del stmt](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement). – Moses Koledoye Jul 02 '20 at 02:02

1 Answers1

3

You can't delete value with del while looping as you can see here, if you want to use del, you should delete the item by its index not the value it takes in the scope of the for loop, because as @MosesKoledoye said in the comments:

You want del players['Tournaments'] for the deletion to act on the dictionary & remove that entry, and not del value which only deletes the value name from the scope. See the del stmt.

When you're looping and you want to modify the list, you have to create a copy, as you can see in the docs. I suggest you to see the link above, to see other ways to delete an element while looping. Try this:

for players in player_data:
    for i in range(len(players['Tournaments'])):
        if players['Tournaments'][i]['Points'] == 0:
            del players['Tournaments'][i]

I prefer to use better dictionary comprehension, so you can try this too:

player_data[0]['Tournaments']=[dct for dct in player_data[0]['Tournaments'] if dct['Points']!=0]
print(data)

Output:

[{'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}], 'Average': 71.19}]
MrNobody33
  • 6,413
  • 7
  • 19
  • Awesome that worked. Do you know why the way I was doing doesn't work? Is there like a special case or something – Austin Johnson Jul 02 '20 at 01:56
  • 1
    @AustinJohnson when you do `del value`, the *reference* to the dictionary gets deleted, but the dictionary object does not get deleted because the list still contains the reference to the dictionary object. – ywbaek Jul 02 '20 at 02:05
  • 1
    @AustinJohnson just added a little explanation of why you method wasn't working. Hope you find it helpful. Also, you could consider accept the answer :). – MrNobody33 Jul 02 '20 at 02:33