Working with JSON Data in Python

That’s really sad. But I shouldn’t have to worry about maintaining of Discord.py. My bot is hosted on Repl.it, and currently the Python version of projects is 3.8.2. If it updates, then I’ll worry.

The issue is with the discord API, eventually the end points used by discord.py will stop working, and the bot will die

That was written back in August last year…

This guy seems to still be working on discord.py. See the commit history. But I don’t know if it’s just maintenance patches or if they are implementing what is necessary to keep going.

Last commit was 5 hours ago. Lol.

Do you possibly know the answer to my question, though? How do I loop through dictionaries retrieved from a .JSON file? (Not values in the dictionary.)

Your dictionaries are stored in a list.
So, you need to iterate over the elements of that list - at this stage, it really doesn’t matter that the list elements contain dictionaries.

But it looks as though you’re trying to delete list elements while you’re iterating over the list using a for loop.
That isn’t a good idea - you’ll get unpredictable results because some list elements will be skipped.

I think that a better approach would be to create a new list, without the elements that you want to delete.

There are a number of different ways to do that, for example:

  • you could define an empty list and then use a for loop , using the list append method to add elements that you want to keep
  • you could use the filter function to produce a new list without the elements you want to delete
  • you could use a list comprehension to produce a new list without the elements you want to delete

For all of the above:

  • remember that your list of dictionaries is accessed via the JSON data key i.e. data['events']
  • you’ll need to assign your new list to the data['events'] key i.e. data['events'] = your_new_list

I hope that helps.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.