0
    path = os.path.dirname(os.path.realpath(__file__))
    if os.path.exists(path + '/nomore.json'):
        blacklist = json.loads(open("%s/nomore.json" % (path)).read())
        if cmd.lower() == "add":
            try:
                username = await self.bot.get_user_info(usr)
                blacklist["blacklist"].append(int(usr))
                json.dump(blacklist, (open("%s/nomore.json" % (path), 'w')))
                await self.bot.say('`%s` added to blacklist' % (username))
            except Exception as e:
                await self.bot.say(e)
        elif cmd.lower() == "remove":
            try:
                for ids in blacklist["blacklist"]:
                    if ids == int(usr):
                        blacklist["blacklist"].remove[ids]
                        json.dump(blacklist, (open("%s/nomore.json" % (path), 'w')
                        await self.bot.say('Remove invoked')
            except Exception as e:
                await self.bot.say(e)

I'm adding values to the JSON object by using .append() but I'm not sure what would be the proper way to remove a value from the array by value.

This is what the JSON file looks like:

{"blacklist": [225915965769121792, 272445925845106700]}
Okiic.
  • 117
  • 2
  • 13

3 Answers3

3

Well... if blacklist is your json, then why not do the following?

# If you know the value to be removed:
blacklist['blacklist'].remove(225915965769121792)
# Or if you know the index to be removed (pop takes an optional index, otherwise defaults to the last one):
blacklist['blacklist'].pop()
# Or
del blacklist['blacklist'][0]  # 0 or any other valid index can be used.
Sam Chats
  • 2,271
  • 1
  • 12
  • 34
0

https://docs.python.org/3/tutorial/datastructures.html

blacklist["blacklist"].pop(i); # `i` is the index you want to remove
0
lst = [100, 200, 300, 400]

idx = lst.index(300)
removed_item = lst.pop(idx)

print(removed_item)
print(lst)
Alex Undefined
  • 620
  • 1
  • 5
  • 8