Last active
December 13, 2015 22:49
-
-
Save akhenakh/4987562 to your computer and use it in GitHub Desktop.
Remove keys from a list of dicts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list comprehension + dict comprehension should be memory efficient and can remove multiple keys | |
# but code is ugly | |
clean_list = [ | |
{k: v | |
for k, v in dict_to_clean.iteritems() | |
if k not in ['tid', 'aid']} | |
for dict_to_clean in my_list] | |
# lambda can remove only one key and I hate lambdas | |
map(lambda d: d.pop('tid'), my_list) | |
# simple for loop can remove multiple keys | |
for d in my_list: | |
del d['tid'] | |
del d['aid'] | |
# What is yours ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my code my_list is not a list but a mongo cursor, so lambda and for loop need a first step.
my_list = list(messages_q)