Difference between del, remove and pop on lists

前端 未结 12 2144
北海茫月
北海茫月 2020-11-22 04:20
>>> a=[1,2,3]
>>> a.remove(2)
>>> a
[1, 3]
>>> a=[1,2,3]
>>> del a[1]
>>> a
[1, 3]
>>> a= [1,2,3]
>         


        
12条回答
  •  有刺的猬
    2020-11-22 04:38

    pop

    Takes index (when given, else take last), removes value at that index, and returns value

    remove

    Takes value, removes first occurrence, and returns nothing

    delete

    Takes index, removes value at that index, and returns nothing

提交回复
热议问题