본문 바로가기

내가 당면한 문제와 해결방안

python find element in dictionary list using key value

https://stackoverflow.com/a/8653568/10194999

 

Python list of dictionaries search

Assume I have this: [ {"name": "Tom", "age": 10}, {"name": "Mark", "age": 5}, {"name": "Pam", "age": 7} ] and by searching "Pam" as name, I want to retrieve the related dictionary: {name: "Pam", ...

stackoverflow.com

dicts = [
    { "name": "Tom", "age": 10 },
    { "name": "Mark", "age": 5 },
    { "name": "Pam", "age": 7 },
    { "name": "Dick", "age": 12 }
]


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

 

generator expression 

을 쓰라구 한다. 이걸 좀 알아둬야겠어 매번 검색하기도 피곤해 ㅠㅠ 

 

 

 

참고

http://schoolofweb.net/blog/posts/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EC%A0%9C%EB%84%88%EB%A0%88%EC%9D%B4%ED%84%B0-generator/