简体   繁体   中英

I don't understand how this line of code works

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM