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", age: 7}
How to achieve this ?
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", age: 7}
How to achieve this ?
You can try this:
You can use a list comprehension:
You can use a generator expression:
@Frédéric Hamidi's answer is great. In Python 3.x the syntax for
.next()
changed slightly. Thus a slight modification:As mentioned in the comments by @Matt, you can add a default value as such:
This looks to me the most pythonic way:
result (returned as a list in Python 2):
Note: In Python 3, a filter object is returned. So the python3 solution would be:
Simply using list comprehension:
Sample code: