Last active
August 28, 2019 08:40
-
-
Save kenoir/02edebbde271385277737b51bc571314 to your computer and use it in GitHub Desktop.
Clean and filter a target dict
This file contains hidden or 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
# 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