Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Last active December 13, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhenakh/4987562 to your computer and use it in GitHub Desktop.
Save akhenakh/4987562 to your computer and use it in GitHub Desktop.
Remove keys from a list of dicts
# 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 ?
@akhenakh
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment