Created
May 10, 2013 10:57
-
-
Save mlalic/b525e92cdda31d6cafc2 to your computer and use it in GitHub Desktop.
Allows multiple duplicate keys in a dict. Not a complete implementation (e.g. __getitem__ missing, etc.) and not the most efficient one; just a proof of concept.
This file contains 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
class DuplicateDict(object): | |
def __init__(self): | |
self._items = [] | |
def items(self): | |
return self._items | |
def __iter__(self): | |
def wrap(): | |
for key, value in self._items: | |
yield key | |
return wrap() | |
def get(self, key, default): | |
# O(n) | |
for k, v in self._items: | |
if k == key: | |
return value | |
return default | |
def add(self, key, value): | |
self._items.append((key, value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment