Python list of dictionaries search

后端 未结 21 2051
-上瘾入骨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:04

    This is a general way of searching a value in a list of dictionaries:

    def search_dictionaries(key, value, list_of_dictionaries):
        return [element for element in list_of_dictionaries if element[key] == value]
    

提交回复
热议问题