How can I use bisect module on lists that are sorted descending? e.g.
import bisect x = [1.0,2.0,3.0,4.0] # normal, ascending bisect.insort(x,2.5) # -->
One approach is to negate the keys. For example,
a = [1,2,3,4] a.reverse() def comparator(a): return -a b = [comparator(i) for i in a] bisect.insort_right(b,comparator(2.5)) a = [comparator(c) for c in b]
Result: [4, 3, 2.5, 2, 1]