How to remove an element from a list by index

后端 未结 18 3232
闹比i
闹比i 2020-11-22 03:22

How do I remove an element from a list by index in Python?

I found the list.remove method, but say I want to remove the last element, how do I do this?

18条回答
  •  执笔经年
    2020-11-22 03:46

    Use the del statement:

    del listName[-N]
    

    For example, if you want to remove the last 3 items, your code should be:

    del listName[-3:]
    

    For example, if you want to remove the last 8 items, your code should be:

    del listName[-8:]
    

提交回复
热议问题