Skip to content

Instantly share code, notes, and snippets.

@willb
Created July 9, 2009 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willb/143743 to your computer and use it in GitHub Desktop.
Save willb/143743 to your computer and use it in GitHub Desktop.
class ord_dict(dict):
""" ord_dict is a dict that remembers the order in which its keys were originally inserted"""
def __init__(self):
self.ord_keys = []
def keys(self):
return list(self.ord_keys)
def __setitem__(self, key, value):
if not self.has_key(key):
self.ord_keys.append(key)
super(ord_dict, self).__setitem__(key, value)
def __iter__(self):
def od_iterator(ls, od):
for val in ls:
yield ((val, od[val]))
return od_iterator(self.ord_keys, self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment