jv95
jv95

Reputation: 691

Remove dict from list of dict if key and value doesnt match with other dict

I have a list of dictionaries and I want to filter it by values from other dictionary.

orig_list = [{"name":"Peter","last_name":"Wick","mail":"[email protected]","number":"111"},
{"name":"John","last_name":"Hen","mail":"[email protected]","number":"222"},
{"name":"Jack","last_name":"Malm","mail":"[email protected]","number":"542"},
{"name":"Anna","last_name":"Hedge","mail":"[email protected]"},
{"name":"Peter","last_name":"Roesner","mail":"[email protected]","number":"445"},
{"name":"Tino","last_name":"Tes","mail":"[email protected]","number":"985"},]

expected result example 1:

filter = {"name":"Peter"}

orig_list[{"name":"Peter","last_name":"Wick","mail":"[email protected]","number":"111"},
{"name":"Peter","last_name":"Roesner","mail":"[email protected]","number":"445"}]

expected result example 2:

filter = {"name":"Peter","number":"445"}

orig_list[
{"name":"Peter","last_name":"Roesner","mail":"[email protected]","number":"445"}]

The filter can have multiple keys. possible keys are(name,last_name,number). Basically what I want, is to go through the list of dict and check every dict if the dict contains key from given filter and if it does, check if the key values match. If they dont, remove the whole dict from the list of dict.

The final list does not have to be the orig_list. It can be a new list. So its not mandatory to delete dicts from the orig_list. The dicts can be also copied to new list of dicts.

Upvotes: 0

Views: 1786

Answers (2)

Alain T.
Alain T.

Reputation: 42139

If you are certain that all the filter dictionary's keys exist in the other dictionaries, you could write the search like this (in other words, absent keys will be considered to match):

filterDict = {"name":"Peter"}
result = [ d for d in orig_list if {**d,**filterDict} == d ]

If the absent keys are not matches, you could do this:

result = [d for d in orig_list if {*filterDict.items()}<={*d.items()}]

Upvotes: 0

You can use list comprehension:

orig_list = [{"name":"Peter","last_name":"Wick","mail":"[email protected]","number":"111"},
{"name":"John","last_name":"Hen","mail":"[email protected]","number":"222"},
{"name":"Jack","last_name":"Malm","mail":"[email protected]","number":"542"},
{"name":"Anna","last_name":"Hedge","mail":"[email protected]"},
{"name":"Peter","last_name":"Roesner","mail":"[email protected]","number":"445"},
{"name":"Tino","last_name":"Tes","mail":"[email protected]","number":"985"},]

filter_by = {"name":"Peter"}


result = [dic for dic in orig_list if all(key in dic and dic[key] == val for key, val in filter_by.items())]
print(result)

Output:

[
  {
    "name": "Peter",
    "last_name": "Wick",
    "mail": "[email protected]",
    "number": "111"
  },
  {
    "name": "Peter",
    "last_name": "Roesner",
    "mail": "[email protected]",
    "number": "445"
  }
]

For filter_by = {"name":"Peter","number":"445"} you get:

[
  {
    "name": "Peter",
    "last_name": "Roesner",
    "mail": "[email protected]",
    "number": "445"
  }
]

Upvotes: 3

Related Questions