Removing key values pairs from a list of dictionaries

Question:

I have a list of dictionaries such as:

[{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]

I need to remove all key values pairs from all dictionaries where the key is equal to mykey1. I could do this by looping through and using the del statement, but I am wondering how I would create a new list using list comprehensions or lambdas which would just remove all key value pairs where the key was mykey1.

Many thanks

Asked By: dublintech

||

Answers:

def new_dict(old_dict):
    n = old_dict.copy()
    n.pop('mykey1',None)
    return n

new_list_of_dict = map(new_dict,list_of_dict)

or

new_list_of_dict = [ new_dict(d) for d in list_of_dict ]

Rather than using del, I opted for dict.pop since pop will suppress the KeyError if the key doesn’t exist.


If you really only want to get certain keys, this becomes a bit easier.

from operator import itemgetter
tuple_keys = ('key1','key2','key3')
get_keys = itemgetter(*tuple_keys)
new_dict_list = [ dict(zip(tuple_keys,get_keys(d)) for d in old_dict_list ] 

which raises KeyError if the keys aren’t in the old dict

Or:

new_dict_list = [ dict( (k,d.get(k,None)) for k in tuple_keys ) for d in old_dict_list ]

which will also add key:None if key isn’t in the old dict. If you don’t want that None, you could do:

new_dict_list = [ dict( (k,d[k]) for k in tuple_keys if k in d) for d in old_dict_list ]

Depending on what percent of the dictionary you’re including/excluding and the size of the dictionaries, this might be slightly faster than the solution by @MartijnPieters.

Answered By: mgilson

If you really want to use a list comprehension, combine it with a dict comprehension:

[{k: v for k, v in d.iteritems() if k != 'mykey1'} for d in mylist]

Substitute .iteritems() for .items() if you are on python 3.

On python 2.6 and below you should use:

[dict((k, v) for k, v in d.iteritems() if k != 'mykey1') for d in mylist]

as the {key: value ...} dict comprehension syntax was only introduced in Python 2.7 and 3.

Answered By: Martijn Pieters

[d.pop('mykey1', None) for d in list]

Answered By: Cameron Sparr

On python 3.5 this works successfully
”’result is my list dict”’

[{key: value for key, value in dict.items() if key != 'EQUITY'} for dict in result]
Answered By: Yagmur SAHIN

You can follow this simple step :

arr = [{'mykey1':'myvalue1', 'mykey2':'myvalue2'}, {'mykey1':'myvalue1a', 'mykey2':'myvalue2a'}]

for i in arr:
    del i["mykey1"]

print(arr)

output:

[{'mykey2': 'myvalue2'}, {'mykey2': 'myvalue2a'}] 
Answered By: Elias Prado

It’s nice to have a one liner list comprehensions but the performance might not be as good as del operation. See my test results below:

%%timeit
dl = [
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2}
]
dl1 = [{key: value for key, value in d.items() if key != 'foo'} for d in dl]

2.78 µs ± 62.7 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

%%timeit
dl = [
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2},
    {"foo": 1, "bar": 2}
]
for row in dl:
    del row['foo']

640 ns ± 13.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Answered By: Tyn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.