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
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:
Or you could just iterate with a condition:
You can do it in multiple ways:
You can use
del
operator to remove a certain book by index:Or, You can use pop() method: