Finding the insertion point in a list

tobiaskk at mac.com tobiaskk at mac.com
Fri Mar 16 15:05:04 EDT 2007


Or like this:

	x = [0, 100, 200, 1000]
	y = 435
	for n, i in enumerate(x):
	    if y < i:
         	n = n - 1
	        break
	x.insert(n + 1, y)

If you decide to stick with

	n = sum ( y>x[i] for i in range(len(x)) ) - 1

Replace it with:

	n = sum(y > i for i in x) - 1

Tobias K.




More information about the Python-list mailing list