Difference between del, remove and pop on lists

前端 未结 12 2169
北海茫月
北海茫月 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:50

    Remove basically works on the value . Delete and pop work on the index

    Remove basically removes the first matching value. Delete deletes the item from a specific index Pop basically takes an index and returns the value at that index. Next time you print the list the value doesnt appear.

提交回复
热议问题