Pradeep Pradeep - 1 year ago 62
Python Question

Filtering List of Dict based of a list of keys in python

I am trying to filter a list of dict based on another list of keys. See the below sample.

# Input list of dict
# Key List
key_lst = ['TABLE_NAME','COLUMN_NAME']

My expected output:


I wrote the below code but it is only returning only 1 dict in the list instead of 3. (as per my example)

new_list = [{k:v for i in lst_dict for k,v in i.items() if k in key_lst}]

I am not sure where I am wrong.. Any suggestion is appreciated!

Answer Source

The way you have written new_list, it looks like you expect it to be a list comprehension, but it is only a list containing a single item, which is a dict comprehension. You probably want to move the iterator over lst_dict out of the dictionary and into the list. It should look like this:

new_list = [{k:v for k,v in i.items() if k in key_lst} for i in lst_dict]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download