Does anybody know if Python has an equivalent to Java\'s SortedSet interface?
Heres what I\'m looking for: lets say I have an object of type foo
, and I know
You can use insort
from the bisect module to insert new elements efficiently in an already sorted list:
from bisect import insort
items = [1,5,7,9]
insort(items, 3)
insort(items, 10)
print items # -> [1, 3, 5, 7, 9, 10]
Note that this does not directly correspond to SortedSet
, because it uses a list. If you insert the same item more than once you will have duplicates in the list.