I asked how to remove an object when a list inside it was empty in this question (Remove object if empty list inside it in Python).
The problem is that I'd like to do this only when the objectId
value is the same. I have others objects with different ids and I don't want to touch it.
Input:
data = [{
"objectID": 10745,
"results": [
{
"model": "AUDI - TT QUATTRO",
"price_str": "4 800 EUR"
}]
},
{
"objectID": 10745,
"results": []
},
"objectID": 10746,
"results": [
{
"model": "Porsche 911",
"price_str": "48 000 EUR"
}]
},
]
My code:
for item in data:
objectId = item["objectID"]
results = item["results"]
def removeDuplicate():
#if objectid are the same
new_data = [item for item in data if item['results']]
data[:] = new_data
removeDuplicate()
Expected output:
[{'objectID': 10745,
'results': [{'model': 'AUDI - TT QUATTRO', 'price_str': '4 800 EUR'}]}, {'objectID': 10746,
'results': [{'model': 'Porsche 911', 'price_str': '48 000 EUR'}]}]