So I have a reference dict
ref = {"a": 1, "b": 2}
and a list of dicts
dict_list = [{"a": 1, "b": 2, "c": "success!"}, {"a": 1, "b": 3, "c": "nope!"}]
What I want is to find the dict in dict_list
which matches the ref
erence, and returns the value c
(i.e. "success!"
). I was able to do this, but I'm not a fan of this solution at all:
In [7]: import pandas as pd
...: def f(ref, dict_list):
...: df = pd.DataFrame.from_records(dict_list)
...: return df.loc[(df["a"] == ref["a"]) & (df["b"] == ref["b"])].c[0]
...:
...: f(ref, dict_list)
Out[7]: 'success!'
if anyone has anything more elegant (ideally in pure python) would be great!