Finding the insertion point in a list

John Machin sjmachin at lexicon.net
Sat Mar 17 14:38:40 CET 2007


On Mar 17, 9:46 pm, Steve Holden <s... at holdenweb.com> wrote:
> Paul Rubin wrote:
> > Steven D'Aprano <s... at REMOVE.THIS.cybersource.com.au> writes:
> >> or even
>
> >> len(filter(lambda t, y=y: y>t, x))
>
> > How about
>
> >    min(i for i,t in enumerate(x) if t >= y)
>
> > or
>
> >    max(i for i,t in enumerate(x) if t <= y)
>
> > Those are actually pretty direct.
>
> How about a solution (like the bisect one suggested almost as soon as
> this thread started) that doesn't iterate over the whole list.
>
> Having said which, for the promoted use cases I agree that the append()
> and sort() paradigm wins hands down until it starts to make a real and
> observable difference to the run time.
>

Unfortunately for sort/append, the OP wants to find the insertion
point -- he hasn't mentioned actually doing the insertion.





More information about the Python-list mailing list