Most efficient way to search in list of dicts

后端 未结 2 565
無奈伤痛
無奈伤痛 2020-12-15 22:23

I have the following list of dicts.

people = [
{\'name\': \"Tom\", \'age\': 10},
{\'name\': \"Mark\", \'age\': 5},
{\'name\': \"Pam\", \'age\': 7}
]
<         


        
2条回答
  •  情话喂你
    2020-12-15 23:08

    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

    • Using next produces a time of 731ns
    • Using the search method produces a time of 361ns
    • And lastly the seach_dictionaries uses 811ns

提交回复
热议问题