Skip to content

Instantly share code, notes, and snippets.

@avdotion
Created July 30, 2018 09:18
Show Gist options
  • Save avdotion/a6d14b3e9066e8af08db009ad6bd4d2b to your computer and use it in GitHub Desktop.
Save avdotion/a6d14b3e9066e8af08db009ad6bd4d2b to your computer and use it in GitHub Desktop.
SortedDict — Python 3 Sorted dictionaty class
from bisect import insort
class SortedDict(dict):
def __init__(self):
super(dict, self).__init__()
self.sorted_keys = list()
def __setitem__(self, key, value):
super(SortedDict, self).__setitem__(key, value)
insort(self.sorted_keys, key)
d = SortedDict()
d[4] = 16
d[5] = 25
d[3] = 9
print([d[item] for item in d.sorted_keys])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment