I have the following list of dicts.
people = [
{'name': "Tom", 'age': 10},
{'name': "Mark", 'age': 5},
{'name': "Pam", 'age': 7}
]
What will be the most optimized way in terms of performance for searching the dicts list. Below are the various methods:
next((item for item in dicts if item["name"] == "Pam"), None)
OR
filter(lambda person: person['name'] == 'Pam', people)
OR
def search(name):
for p in people:
if p['name'] == name:
return p
OR
def search_dictionaries(key, value, list_of_dictionaries):
return [element for element in list_of_dictionaries if element[key] == value]
Any other method is also welcome. Thank.
source
share