Skip to content

Commit 3617650

Browse files
thesagarsehgalantmarakis
authored andcommitted
update in multimap function in utils.py and added test for it (#1014)
* update in multimap functi n in utils.py and added test for it * made changes according to PEP style * broke the long sentence to 2 shorter sentences
1 parent 44ea2ee commit 3617650

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

tests/test_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def test_count():
3333
assert count([True, False, True, True, False]) == 3
3434
assert count([5 > 1, len("abc") == 3, 3+1 == 5]) == 2
3535

36+
def test_multimap():
37+
assert multimap([(1, 2),(1, 3),(1, 4),(2, 3),(2, 4),(4, 5)]) == \
38+
{1: [2, 3, 4], 2: [3, 4], 4: [5]}
39+
assert multimap([("a", 2), ("a", 3), ("a", 4), ("b", 3), ("b", 4), ("c", 5)]) == \
40+
{'a': [2, 3, 4], 'b': [3, 4], 'c': [5]}
3641

3742
def test_product():
3843
assert product([1, 2, 3, 4]) == 24

utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def count(seq):
4242

4343
def multimap(items):
4444
"""Given (key, val) pairs, return {key: [val, ....], ...}."""
45-
result = defaultdict(list)
45+
result = collections.defaultdict(list)
4646
for (key, val) in items:
4747
result[key].append(val)
48-
return result
48+
return dict(result)
4949

5050
def multimap_items(mmap):
5151
"""Yield all (key, val) pairs stored in the multimap."""

0 commit comments

Comments
 (0)