Aayla Fetzer :
I have a JSON array and each object has a number representing a category. For example:
data: [
{
"code": "336413"
},
{
"code": "336413"
},
{
"code": "331318"
},
What would be the most efficient way to pick out the objects with a specific code number and delete or leave behind the rest? My dataset has thousands of objects, so efficiency is important.
Errol :
Try this using generator expression to get the values you want.
test_list= [
{
"code": "333413"
},
{
"code": "333413"
},
{
"code": "331318"
}]
code = "333413"
a = (item for item in test_list if item["code"] == code)
print(list(a))
output:
>>[{'code': '333413'}, {'code': '333413'}]