my example data:
list_of_dict =[{'cena': 23, 'nazwa': 'item1', 'param': 'pampam'},
{'cena': 26, 'nazwa': 'item2', 'param': 'iko' },
{'cena': 26, 'nazwa': 'item2a','param': 'ik2' },
{'cena': 26, 'nazwa': 'item2b','param': 'ik2' },
{'cena': 17, 'nazwa': 'item3', 'param': 'etr' },
{'cena': 17, 'nazwa': 'item4', 'param': 'asdf' }]
conditions = {'cena': 26, 'param': 'ik2' }
I tried:
if conditions in list_of_dict:
do_something()
it works, but only when whole conditions dict (every key) matches the one from dict list, I mean:
In [1]: exampleSet = [{ 'type' : 'type1', 'k' : 'kval'},
...: { 'type' : 'type2', 'k' : 'kv2' },
...: { 'type' : 'type2', 'k' : 'k3' },
...: { 'type' : 'type3', 'k' : 'k3' }]
...:
...: conditions = { 'type' : 'type1', 'k' : 'kval' }
...:
...:
...: conditions in exampleSet
...:
Out[1]: True
In [2]: conditions = { 'type' : 'type1' }
In [3]: conditions in exampleSet
Out[3]: False
while I am trying to match dictionaries with key-value pairs specified, (regardless of values/existence of unspecified ones) so
In [4]: exampleSet = [{ 'type' : 'type1', 'k' : 'kval'},
...: { 'type' : 'type2', 'k' : 'kv2' },
...: { 'type' : 'type2', 'k' : 'k3' },
...: { 'type' : 'type3', 'k' : 'k3' }]
...:
...: conditions = { 'type' : 'type2' }
...:
...: my_wanted_match( exampleSet, conditions )
has to return:
[{ 'type' : 'type2', 'k' : 'kv2' },
{ 'type' : 'type2', 'k' : 'k3' }]
as a result.
can anyone gimme some hints on how to achieve this?
We can easily search a list of dictionaries for an item by using the filter() function with a lambda function. In Python3, the filter() function returns an object of the filter class. We can convert that object into a list with the list() function.
tl;dr. With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.
Use a list comprehension to find the values of a key in a list of dictionaries. Use the list comprehension syntax [dict[key] for dict in list_of_dicts] to get a list containing the corresponding value for each occurrence of key in the list of dictionaries list_of_dicts .
To merge multiple dictionaries, the most Pythonic way is to use dictionary comprehension {k:v for x in l for k,v in x. items()} to first iterate over all dictionaries in the list l and then iterate over all (key, value) pairs in each dictionary.
It is a filter()
you want - you want to filter your list-of-dicts based on some condition; returning only the entries that match all criteria.
>>> list_of_dict =[{'cena': 23, 'nazwa': 'item1', 'param': 'pampam'},
... {'cena': 26, 'nazwa': 'item2', 'param': 'iko' },
... {'cena': 26, 'nazwa': 'item2a','param': 'ik2' },
... {'cena': 26, 'nazwa': 'item2b','param': 'ik2' },
... {'cena': 17, 'nazwa': 'item3', 'param': 'etr' },
... {'cena': 17, 'nazwa': 'item4', 'param': 'asdf' }]
Set conditions:
>>> conditions = {'param':'iko'}
And do a one-line filter:
>>> filter(lambda item: all((item[k]==v for (k,v) in conditions.iteritems())), list_of_dict)
[{'cena': 26, 'param': 'iko', 'nazwa': 'item2'}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With