I have the following list of dicts.
people = [
{'name': "Tom", 'age': 10},
{'name': "Mark", 'age': 5},
{'name': "Pam", 'age': 7}
]
Which would be the most optimized way in terms of performance to search in list of dicts. Following are different some 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. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…