est est - 1 year ago 44
Python Question

How to find an index at which a new item can be inserted into sorted list and keep it sorted?

a = 132

b = [0, 10, 30, 60, 100, 150, 210, 280, 340, 480, 530]


I want to know that
a
should be in the 6th position in ordered list
b
.

What's the most pythonic way to do so?

Answer Source

Use bisect. It's not the most beautiful API, but it's exactly what you need.

You'll want to use bisect.bisect, which returns exactly what you want.