Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filtering Dictionary in Python
class FilterableDict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
def filter(self, predicate):
key_copy = tuple(self.keys()) # use Tuple to further reduce memory footprint
for k in key_copy:
if predicate(k, self.get(k)):
del self[k]
return self
def __repr__(self):
return "FilterableDict({})".format(super().__repr__())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.