Most efficient way to query a json array?

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'}]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170516&siteId=1