How to remove an element from a list by index

后端 未结 18 2809
闹比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 04:08

    You probably want pop:

    a = ['a', 'b', 'c', 'd']
    a.pop(1)
    
    # now a is ['a', 'c', 'd']
    

    By default, pop without any arguments removes the last item:

    a = ['a', 'b', 'c', 'd']
    a.pop()
    
    # now a is ['a', 'b', 'c']
    

提交回复
热议问题