I have the following list of dicts.
people = [
{\'name\': \"Tom\", \'age\': 10},
{\'name\': \"Mark\", \'age\': 5},
{\'name\': \"Pam\", \'age\': 7}
]
<
Doing a quick timeit on the functions show that using filter seems to be the fastest of all the methods
%timeit filter(lambda person: person['name'] == 'Pam', people)
1000000 loops, best of 3: 263 ns per loop
If you are searching for a single item then this is the "best" approach
def search(name):
for p in people:
if p['name'] == name:
return p
All the other implementations will iterate over all the items in the list, whereas this one will stop once the item is found