Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python filtering list of dict by dict containing several key-value pairs as conditions

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?

like image 534
internety Avatar asked Apr 09 '16 23:04

internety


People also ask

How do I filter a dictionary list in Python?

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.

Should I use dict () or {}?

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.

How do you find the values of a key in a list of dictionaries in Python?

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 .

How do I merge a list of Dicts into a single dict?

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.


1 Answers

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'}]
like image 146
haavee Avatar answered Sep 21 '22 04:09

haavee