Skip to content

Instantly share code, notes, and snippets.

@Nyahua
Created March 30, 2017 14:12
Show Gist options
  • Save Nyahua/580a812ee73c3ea7fc268bc8965fc52d to your computer and use it in GitHub Desktop.
Save Nyahua/580a812ee73c3ea7fc268bc8965fc52d to your computer and use it in GitHub Desktop.
how to get a subset / diff from dict
def dict_inter(dict_in, flds):
dict_out = {
key: dict_in[key] for key in flds if key in dict_in.keys()
}
return dict_out
def dict_diff(dict_in, flds):
dict_out = {
key: dict_in[key] for key in dict_in.keys() if key not in flds
}
return dict_out
flds = ['a', 'b', 'c']
dict_in = {'a': 1, 'b': 2, 'd': 4, 'e': 5 }
print(dict_inter(dict_in, flds))
print(dict_diff(dict_in, flds))
# {'a': 1, 'b': 2}
# {'d': 4, 'e': 5}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment