Skip to content

Commit

Permalink
Merge pull request #257 from eigenhombre/more-arities-for-dissoc
Browse files Browse the repository at this point in the history
Add multiple key arguments to dissoc.
  • Loading branch information
John Jacobsen committed Aug 1, 2015
2 parents 330f827 + 9126df9 commit 46af29c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions toolz/dicttoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,21 @@ def assoc(d, key, value, factory=dict):
return merge(d, d2, factory=factory)


def dissoc(d, key):
def dissoc(d, *keys):
"""
Return a new dict with the given key removed.
Return a new dict with the given key(s) removed.
New dict has d[key] deleted.
New dict has d[key] deleted for each supplied key.
Does not modify the initial dictionary.
>>> dissoc({'x': 1, 'y': 2}, 'y')
{'x': 1}
>>> dissoc({'x': 1, 'y': 2}, 'y', 'x')
{}
"""
d2 = copy.copy(d)
del d2[key]
for key in keys:
del d2[key]
return d2


Expand Down
1 change: 1 addition & 0 deletions toolz/tests/test_dicttoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def test_dissoc(self):
assert dissoc(D({"a": 1}), "a") == D({})
assert dissoc(D({"a": 1, "b": 2}), "a") == D({"b": 2})
assert dissoc(D({"a": 1, "b": 2}), "b") == D({"a": 1})
assert dissoc(D({"a": 1, "b": 2}), "a", "b") == D({})

# Verify immutability:
d = D({'x': 1})
Expand Down

0 comments on commit 46af29c

Please sign in to comment.