I have two corresponding lists:
- One is a list of items
- The other is a list of labels for those items
I want to write a function that check if each item in the 1st list is a JSON object. If it is, it should remain in the list but if not, then it and its corresponding label should be deleted.
I wrote the following script to do this:
import json
def check_json (list_of_items, list_of_labels):
for item in list_of items:
try:
json.loads(item)
break
except ValueError:
index_item = list_of_items.index(item)
list_of_labels.remove(index_item)
list_of_items.remove(index_item)
However, it does not remove items that are not JSON objects.