0 votes
1 view
in Python by (14.4k points)

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?

1 Answer

0 votes
by (31.1k points)

If you want to search from Python list of dictionaries then you can use a generator expression below is the code for that shows how to use the generator expression:-

Generator Expression:-

A generator expression is a high performance, memory efficient and generalization of list comprehensions.

dicts = [ 

{"name": "Tom", "age": 10}, 

{"name": "Mark", "age": 5}, 

{"name": "Pam", "age": 7} 

]

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

image

...