Python list of dictionaries search

后端 未结 21 2458
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:41

Assume I have this:

[
{\"name\": \"Tom\", \"age\": 10},
{\"name\": \"Mark\", \"age\": 5},
{\"name\": \"Pam\", \"age\": 7}
]

and by searchin

21条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 10:15

    To add just a tiny bit to @FrédéricHamidi.

    In case you are not sure a key is in the the list of dicts, something like this would help:

    next((item for item in dicts if item.get("name") and item["name"] == "Pam"), None)
    

提交回复
热议问题