Python list for dictionary search

Suppose I have this:

{"name": "Tom", "age": 10},
{"name": "Mark", "age": 5},
{"name": "Pam", "age": 7}

And by searching for "Pam" as the name, I want to retrieve the relevant Dictionaries: {name: "Pam", age: 7}

How to achieve it?

#1 building

This is the general way to search for values in the dictionary list:

def search_dictionaries(key, value, list_of_dictionaries):
    return [element for element in list_of_dictionaries if element[key] == value]

#2 building

In my opinion, this is the most Python way:

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

filter(lambda person: person['name'] == 'Pam', people)

Results (returned as a list in Python 2):

[{'age': 7, 'name': 'Pam'}]

Note: in Python 3, a filter object is returned. So the python 3 solution will be:

list(filter(lambda person: person['name'] == 'Pam', people))

#3 building

@Fr é d é ric Hamidi's answer was very good. In Python 3.x, the syntax of. next() changes slightly. Therefore, it is slightly modified:

>>> dicts = [
     { "name": "Tom", "age": 10 },
     { "name": "Mark", "age": 5 },
     { "name": "Pam", "age": 7 },
     { "name": "Dick", "age": 12 }
>>> next(item for item in dicts if item["name"] == "Pam")
{'age': 7, 'name': 'Pam'}

As described in @ Matt's comments, you can add default values like this:

>>> next((item for item in dicts if item["name"] == "Pam"), False)
{'name': 'Pam', 'age': 7}
>>> next((item for item in dicts if item["name"] == "Sam"), False)

#4 building

Add a little bit to @ Fr é d é ricHamidi.

If you are not sure if a key is in the dictionary list, you can use the following methods:

next((item for item in dicts if item.get("name") and item["name"] == "Pam"), None)

#5 building

This is a comparison using iterated traversal lists, using filter + lambda or refactored (if needed or in your case) code to use your code for commands, not command lists

import time

# Build list of dicts
list_of_dicts = list()
for i in range(100000):
    list_of_dicts.append({'id': i, 'name': 'Tom'})

# Build dict of dicts
dict_of_dicts = dict()
for i in range(100000):
    dict_of_dicts[i] = {'name': 'Tom'}

# Find the one with ID of 99

# 1. iterate through the list
lod_ts = time.time()
for elem in list_of_dicts:
    if elem['id'] == 99999:
lod_tf = time.time()
lod_td = lod_tf - lod_ts

# 2. Use filter
f_ts = time.time()
x = filter(lambda k: k['id'] == 99999, list_of_dicts)
f_tf = time.time()
f_td = f_tf- f_ts

# 3. find it in dict of dicts
dod_ts = time.time()
x = dict_of_dicts[99999]
dod_tf = time.time()
dod_td = dod_tf - dod_ts

print 'List of Dictionries took: %s' % lod_td
print 'Using filter took: %s' % f_td
print 'Dict of Dicts took: %s' % dod_td

The output is as follows:

List of Dictionries took: 0.0099310874939
Using filter took: 0.0121960639954
Dict of Dicts took: 4.05311584473e-06

Conclusion: in these cases, it is obvious that having a dictionary dictionary is the most effective way to search. In this case, you know that you will only search by id. Interestingly, using filters is the slowest solution.

Tags: Python Lambda

Posted on Fri, 14 Feb 2020 10:40:30 -0500 by Poofman