Skip to content

Instantly share code, notes, and snippets.

@arbennett
Created May 26, 2016 20:21
Show Gist options
  • Save arbennett/8768010e3db6d4c29f52f2fe2a50ccb8 to your computer and use it in GitHub Desktop.
Save arbennett/8768010e3db6d4c29f52f2fe2a50ccb8 to your computer and use it in GitHub Desktop.
class scdict(dict):
"""
Separate chaining dictionary implementation allows
for multiple values to be assigned to a single key
in a list.
"""
def __setitem__(self, key, val):
"""
Sets a value to a key, and if the key already exists,
make a list of all of the values assigned
"""
newval = val
if key in self:
oldval = self.__getitem__(key)
if type(oldval) == type([]):
newval = oldval + [newval]
else:
newval = [oldval, val]
dict.__setitem__(self, key, newval)
def testit():
""" Just show it works """
td = scdict()
td['first'] = 'this'
td['first'] = 'might'
td['first'] = 'work'
print(td)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment