How to remove an element from a list by index

后端 未结 18 2943
闹比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:47

    You can use either del or pop to remove element from list based on index. Pop will print member it is removing from list, while list delete that member without printing it.

    >>> a=[1,2,3,4,5]
    >>> del a[1]
    >>> a
    [1, 3, 4, 5]
    >>> a.pop(1)
     3
    >>> a
    [1, 4, 5]
    >>> 
    

提交回复
热议问题