I wanted to sort a list in-place and tried using the list itself during the sorting (within a key
function). I found out that the list itself appears to be empty in there.
a = [1,4,5,3,2,6,0]
b = ['b', 'e', 'f', 'd', 'c', 'g', 'a']
b.sort(key=lambda x: a[b.index(x)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
ValueError: 'b' is not in list
So I tried:
def f(x):
print "FOO:", x, b
return b.index(x)
b.sort(key=f)
and got
FOO: b []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
ValueError: 'b' is not in list
Any explanation for this?