Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to search in list of dicts

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.

like image 339
Andy M. Avatar asked Aug 10 '16 05:08

Andy M.


2 Answers

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
like image 165
Varun Avatar answered Sep 28 '22 18:09

Varun


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

like image 41
Leon Avatar answered Sep 28 '22 16:09

Leon