Skip to content

Instantly share code, notes, and snippets.

@mlalic
Created May 10, 2013 10:57
Show Gist options
  • Save mlalic/b525e92cdda31d6cafc2 to your computer and use it in GitHub Desktop.
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.
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