P
Paul Whipp
Hi there,
I'm new to Python so apologies if this is too naive...
I wanted to recurse down a list to do an ordered insert. All was well
until...
def splung(l,x):
if l[1:]==[]:
l[1:]=[x]
elif x>l[1]:
splung(l[1:],x)
else:
l[1:1]=[x]
It took me a while to divine that the l[1:] is destroyed if its an lvalue
but returns a copy when used as an argument parameter. If that is the case,
then is there an effective way to recurse and destructively do an ordered
insert on a list?
Cheers,
Paul
I'm new to Python so apologies if this is too naive...
I wanted to recurse down a list to do an ordered insert. All was well
until...
def splung(l,x):
if l[1:]==[]:
l[1:]=[x]
elif x>l[1]:
splung(l[1:],x)
else:
l[1:1]=[x]
foo=[1]
splung(foo,2)
foo [1, 2]
splung(foo,3)
foo [1, 2]
It took me a while to divine that the l[1:] is destroyed if its an lvalue
but returns a copy when used as an argument parameter. If that is the case,
then is there an effective way to recurse and destructively do an ordered
insert on a list?
Cheers,
Paul