Description
Range key deletion feature is currently missing for SortedDict
. If I want to delete multiple keys within an range, I have to do them one by one using a for loop which is very inefficient in terms of time complexity. See my example below.
# program that removes the subdict from 2 to 4
d = SortedDict({1: 1, 2: 2, 3: 3, 4: 4, 5: 5})
lower_bound_index = d.bisect_left(2)
upper_bound_index = d.bisect_left(4)
for _ in range(lower_bound_index, upper_bound_index + 1):
d.popitem(lower_bound_index)
This is very inefficient both in terms of usage and in terms of time complexity. Time complexity for this one is O(k log n) where k is the range size. Other languages such as C++ and Java only needs an time complexity of O(k + log n) to accomplish the job with the API mentioned below.
In C++, you have the option of calling erase(iterator first, iterator last)
for their map
data structure. Similarly, in Java, you can call subMap(first, last).clear()
for their TreeMap
data structure. Something similar should be available for SortedDict
in my opinion.