Skip to content

Instantly share code, notes, and snippets.

@kenoir
Last active August 28, 2019 08:40
Show Gist options
  • Save kenoir/02edebbde271385277737b51bc571314 to your computer and use it in GitHub Desktop.
Save kenoir/02edebbde271385277737b51bc571314 to your computer and use it in GitHub Desktop.
Clean and filter a target dict
# Usage:
#
# target_dict = {
# 'foo': 'value'
# 'baz': 'another_value'
# }
#
# attributes = ['foo', 'bar']
# new_dict = get_attributes(target_dict
# assert new_dict == { 'foo': 'value' }
#
def get_attributes(target_dict, attributes):
return {attr: target_dict.get(attr) for attr in attributes if target_dict.get(attr)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment