Reputation: 3
list = [x*x for x in range(5)]
def fun(L):
del L[L[2]]
return L
print(fun(list))
Hello, I need help understanding this line of code: del L[L[2]]. I understand how just del L[2] would result in an answer of 0, 1, 9, 16 (deleting the 2nd positioned list-item), however I'm not sure how, in the actual answer of 0, 1, 4, 9, the 16 gets deleted. That first 'L' is what I'd like to understand. Is it multiplying the list index by 2 and deleting the 4th item? If so, how does that work? Thanks very much for your help.
Upvotes: -1
Views: 63
Reputation: 791
The list L
looks like [0, 1, 4, 9, 16]
. L[2]
is 4
. So basically, you are deleting L[L[2]]
or L[4]
which is 16
.
PS : It's not a good practice to use keywords as variable names. Here, list
is not a good variable name.
Upvotes: 3