0

I have a JSON file and would like to remove a certain book

[
  {
    "title": "How to love",
    "author": "vagabondage3",
    "isbn": 9832,
    "genre": "Comedy",
    "numofcopies": 2
  },
  {
    "title": "How to eat",
    "author": "belabor",
    "isbn": 2345,
    "genre": "Documentary",
    "numofcopies": 3
  },
  {
    "title": "hwo",
    "author": "TafaTIC",
    "isbn": 19,
    "genre": "Comedy",
    "numofcopies": 9
  }
]

Part to remove

{
    "title": "hwo",
    "author": "TafaTIC",
    "isbn": 19,
    "genre": "Comedy",
    "numofcopies": 9
  }

the json file should look like this in the end

[
  {
    "title": "How to love",
    "author": "vagabondage3",
    "isbn": 9832,
    "genre": "Comedy",
    "numofcopies": 2
  },
  {
    "title": "How to eat",
    "author": "belabor",
    "isbn": 2345,
    "genre": "Documentary",
    "numofcopies": 3
  }
]

I honestly saw a similar example but I can't understand it can you explain please how it was done thanks in advance <3

2 Answers 2

3

In the end it depends how you identify which one you want to delete. If you want to delete by index, you could try this:

json_data.pop(2)

Or you could just iterate with a condition:

json_data = [item for item in json_data if item['title'] != 'hwo']
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it in multiple ways: You can use del operator to remove a certain book by index:

del data_json[2]

Or, You can use pop() method:

data_json.pop(2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.